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

Discussion in 'C#' started by diac, Oct 4, 2006.

  1. #1
    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 [B]xxx [/B]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 = <%=[B]xxx[/B]%>
    
    
    Code (markup):
    Thx in advance
     
    diac, Oct 4, 2006 IP
  2. Free Born John

    Free Born John Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    declare it at module level ie right at the top of the class, then it will have scope throughout the module (the m_ prefix is used here as good practice to indicate a module level variable)

    Partial Class imagegallery
    Inherits System.Web.UI.Page
    Private m_xxxx As String

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    m_xxx = "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 = <%=m_xxx%>
     
    Free Born John, Oct 4, 2006 IP
  3. diac

    diac Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you :)
     
    diac, Oct 5, 2006 IP