你好我正在使用这个Page.ClientScript.RegisterStartupScript()从后面的vb代码调用一个javascript函数,这对我很好用我的问题是如何从后面的代码发送变量到javascript函数这里是什么我到目前为止尝试过:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.LoadComplete
Dim Myname As String = "myName"
Dim cstype As Type = Me.GetType()
Page.ClientScript.RegisterStartupScript(cstype, "MyKey", "hello(Myname);",
True)
End Sub
javascript:
<script type="text/javascript">
function hello(name) {
alert("hello world from javascript " + name)
}
</script>
谢谢...
答案 0 :(得分:2)
您必须使用正确的引号来传递字符串:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.LoadComplete
Dim Myname As String = "myName"
Dim cstype As Type = Me.GetType()
Page.ClientScript.RegisterStartupScript(cstype, "MyKey", "hello('" & Myname & "');",
True)
End Sub
注意变量名称周围的单引号。
在客户端和服务器端代码之间双向传递数据的另一种方法是隐藏字段,例如
<asp:HiddenField ID="xhidMyname" runat="server" />
或
<input type="hidden" id="xhidMyname" runat="server" />
此字段可通过其“value”属性在客户端和服务器端访问。
答案 1 :(得分:1)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.LoadComplete
Dim Myname As String = "myName"
Dim cstype As Type = Me.GetType()
Page.ClientScript.RegisterStartupScript(cstype, "MyKey", "hello('"&Myname&"');",
True)
End Sub