Fill a javascript var with the content of a variable declared in a vb.net Sub

Discussion in 'ASP .Net' started by diac, Oct 4, 2006.

  1. diac

    diac Guest

    Hello,

    I create and fill a variable in the Page_Load sub and then i need it in
    a javascript code. Of course the xxx variable is declared in the sub so
    that it is not available out of the sub. How can i fill the javascript
    variable with its content?

    ------------------------------------
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim xxx As String = "xxx"
    End Sub

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>PR</title>
    <script type="text/javascript" language="javascript">

    var xxx = <%=xxx%>
    ------------------------------------

    Thx in advance
     
    diac, Oct 4, 2006
    #1
    1. Advertising

  2. Hi Daniele,

    There are methods built into ASP.NET for outputing script. Try the code
    below?

    Ken
    Microsoft MVP [ASP.NET]

    <%@ Page Language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
    Protected Sub Page_Load _
    (ByVal sender As Object, _
    ByVal e As System.EventArgs)
    Dim xxx As Integer
    xxx = 666
    ClientScript.RegisterClientScriptBlock _
    (Me.GetType, _
    "var", "var xxx=" & xxx.ToString & ";", _
    True)
    End Sub
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body onload="alert(xxx);">
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
    </body>
    </html>

    "diac" <> wrote in message
    news:...
    > Hello,
    >
    > I create and fill a variable in the Page_Load sub and then i need it in
    > a javascript code. Of course the xxx variable is declared in the sub so
    > that it is not available out of the sub. How can i fill the javascript
    > variable with its content?
    >
    > ------------------------------------
    > Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    > Dim xxx As String = "xxx"
    > End Sub
    >
    > <html xmlns="http://www.w3.org/1999/xhtml" >
    > <head runat="server">
    > <title>PR</title>
    > <script type="text/javascript" language="javascript">
    >
    > var xxx = <%=xxx%>
    > ------------------------------------
    >
    > Thx in advance
    >
     
    Ken Cox [Microsoft MVP], Oct 4, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred
    Replies:
    3
    Views:
    341
    Alf P. Steinbach
    Aug 10, 2003
  2. Ben
    Replies:
    2
    Views:
    954
  3. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,166
    Lawrence D'Oliveiro
    May 20, 2011
  4. steve
    Replies:
    2
    Views:
    146
    steve
    Aug 16, 2005
  5. Keiron Waites

    var.innerHTML where var is a variable!!

    Keiron Waites, Aug 11, 2003, in forum: Javascript
    Replies:
    2
    Views:
    87
    Martin Honnen
    Aug 11, 2003
Loading...

Share This Page