简体   繁体   English

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

[英]passing variable from VB function to client side javascript

hello i am using this Page.ClientScript.RegisterStartupScript() to call a javascript function from vb code behind , and this works fine with me My question is how can i send variables from the code behind to the javascript function here is what i have tried so far : 您好我正在使用此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 : javascript:

   <script type="text/javascript">
    function hello(name) {
        alert("hello world from javascript " + name)
    }
</script>

Thank you ... 谢谢 ...

You have to use correct quotations to pass strings: 您必须使用正确的引用来传递字符串:

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 

Note single quotes around variable name. 注意变量名称周围的单引号。

Another way for bi-directional passing of data between client and server-side code is hidden fields, eg 在客户端和服务器端代码之间双向传递数据的另一种方式是隐藏字段,例如

<asp:HiddenField ID="xhidMyname" runat="server" />

or 要么

<input type="hidden" id="xhidMyname" runat="server" />

This field will be accessible both client-side and server side via it's "value" property. 客户端和服务器端都可以通过它的“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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM