C# 将变量从VB函数传递到客户端javascript

C# 将变量从VB函数传递到客户端javascript,c#,javascript,asp.net,.net,vb.net,C#,Javascript,Asp.net,.net,Vb.net,您好,我正在使用此页面.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

您好,我正在使用此页面.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>

函数hello(name){
警报(“来自javascript的hello world”+名称)
}

谢谢…

您必须使用正确的引号来传递字符串:

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 
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" />



此字段可以通过其“value”属性在客户端和服务器端访问。

您必须使用正确的引号来传递字符串:

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" />



此字段将通过其“value”属性在客户端和服务器端均可访问。

ya,很抱歉我没有在字符串周围添加单引号ya,很抱歉我没有在字符串周围添加单引号