how to call aspx variable from Javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jade

    how to call aspx variable from Javascript

    I saw some web page saying I could do it this way in
    javascript:
    var iNumber = <%#publicvarnam e [or] publicpropertyn ame %>

    but it doesn't seem to work. I have this piece of code
    here in javascript:
    <script language="javas cript">
    var sessionServer = "<%#jsTestServe r%>";
    alert(sessionSe rver);
    </script>

    and the variable is defined in the C# code behind:
    public string jsTestServer
    = "\\\\TestServer \\TestSession\\ ";

    when I run it, alert has no message in it.
  • Jim Cheshire [MSFT]

    #2
    RE: how to call aspx variable from Javascript

    Jade,

    Assuming that variable is in scope, you would use the following:

    <% = jsTestServer %>

    Actually, what you should do is use RegisterClientS criptBlock to generate
    your client-side script on the server and then register it on the client.

    Jim Cheshire [MSFT]
    Developer Support
    ASP.NET
    jamesche@online .microsoft.com

    This post is provided as-is with no warranties and confers no rights.

    --------------------[color=blue]
    >Content-Class: urn:content-classes:message
    >From: "Jade" <anonymous@disc ussions.microso ft.com>
    >Sender: "Jade" <anonymous@disc ussions.microso ft.com>
    >Subject: how to call aspx variable from Javascript
    >Date: Tue, 11 Nov 2003 09:30:27 -0800
    >Lines: 16
    >Message-ID: <0dd401c3a879$7 f220130$a001280 a@phx.gbl>
    >MIME-Version: 1.0
    >Content-Type: text/plain;
    > charset="iso-8859-1"
    >Content-Transfer-Encoding: 7bit
    >X-Newsreader: Microsoft CDO for Windows 2000
    >X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
    >Thread-Index: AcOoeX8iZVlOZ5x 5TzC4ue9n01Hi8w ==
    >Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
    >Path: cpmsftngxa06.ph x.gbl
    >Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.framew ork.aspnet:1901 14
    >NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
    >X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
    >
    >I saw some web page saying I could do it this way in
    >javascript:
    >var iNumber = <%#publicvarnam e [or] publicpropertyn ame %>
    >
    >but it doesn't seem to work. I have this piece of code
    >here in javascript:
    ><script language="javas cript">
    > var sessionServer = "<%#jsTestServe r%>";
    > alert(sessionSe rver);
    ></script>
    >
    >and the variable is defined in the C# code behind:
    >public string jsTestServer
    >= "\\\\TestServer \\TestSession\\ ";
    >
    >when I run it, alert has no message in it.
    >[/color]

    Comment

    • Jade

      #3
      RE: how to call aspx variable from Javascript

      the thing is I need to do it on the client side. I've
      tried <% = jsTestServer %> before. It didn't work. It
      didn't even pop up the alert this time. Or maybe I did
      something wrong. Could you please give me a more
      complete piece of code?

      Thanks!
      [color=blue]
      >-----Original Message-----
      >Jade,
      >
      >Assuming that variable is in scope, you would use the[/color]
      following:[color=blue]
      >
      ><% = jsTestServer %>
      >
      >Actually, what you should do is use[/color]
      RegisterClientS criptBlock to generate[color=blue]
      >your client-side script on the server and then register[/color]
      it on the client.[color=blue]
      >
      >Jim Cheshire [MSFT]
      >Developer Support
      >ASP.NET
      >jamesche@onlin e.microsoft.com
      >
      >This post is provided as-is with no warranties and[/color]
      confers no rights.[color=blue]
      >
      >--------------------[color=green]
      >>Content-Class: urn:content-classes:message
      >>From: "Jade" <anonymous@disc ussions.microso ft.com>
      >>Sender: "Jade" <anonymous@disc ussions.microso ft.com>
      >>Subject: how to call aspx variable from Javascript
      >>Date: Tue, 11 Nov 2003 09:30:27 -0800
      >>Lines: 16
      >>Message-ID: <0dd401c3a879$7 f220130$a001280 a@phx.gbl>
      >>MIME-Version: 1.0
      >>Content-Type: text/plain;
      >> charset="iso-8859-1"
      >>Content-Transfer-Encoding: 7bit
      >>X-Newsreader: Microsoft CDO for Windows 2000
      >>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
      >>Thread-Index: AcOoeX8iZVlOZ5x 5TzC4ue9n01Hi8w ==
      >>Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
      >>Path: cpmsftngxa06.ph x.gbl
      >>Xref: cpmsftngxa06.ph x.gbl[/color][/color]
      microsoft.publi c.dotnet.framew ork.aspnet:1901 14[color=blue][color=green]
      >>NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
      >>X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
      >>
      >>I saw some web page saying I could do it this way in
      >>javascript:
      >>var iNumber = <%#publicvarnam e [or] publicpropertyn ame %[/color]
      >[color=green]
      >>
      >>but it doesn't seem to work. I have this piece of code
      >>here in javascript:
      >><script language="javas cript">
      >> var sessionServer = "<%#jsTestServe r%>";
      >> alert(sessionSe rver);
      >></script>
      >>
      >>and the variable is defined in the C# code behind:
      >>public string jsTestServer
      >>= "\\\\TestServer \\TestSession\\ ";
      >>
      >>when I run it, alert has no message in it.
      >>[/color]
      >
      >.
      >[/color]

      Comment

      • Jim Cheshire [MSFT]

        #4
        RE: how to call aspx variable from Javascript

        Jade,

        A complete source sample is not going to help you because the problem is
        almost certainly that jsTestServer is out of scope. That's why using this
        type of legacy architecture is not recommended in ASP.NET. Instead, you
        need to use RegisterClientS criptBlock to create the client-side code on the
        server and then write it to the page.

        Jim Cheshire [MSFT]
        Developer Support
        ASP.NET
        jamesche@online .microsoft.com

        This post is provided as-is with no warranties and confers no rights.

        --------------------[color=blue]
        >Content-Class: urn:content-classes:message
        >From: "Jade" <anonymous@disc ussions.microso ft.com>
        >Sender: "Jade" <anonymous@disc ussions.microso ft.com>
        >References: <0dd401c3a879$7 f220130$a001280 a@phx.gbl>[/color]
        <hHyZb2HqDHA.20 88@cpmsftngxa06 .phx.gbl>[color=blue]
        >Subject: RE: how to call aspx variable from Javascript
        >Date: Tue, 11 Nov 2003 10:12:37 -0800
        >Lines: 72
        >Message-ID: <02a801c3a87f$6 3068330$a301280 a@phx.gbl>
        >MIME-Version: 1.0
        >Content-Type: text/plain;
        > charset="iso-8859-1"
        >Content-Transfer-Encoding: 7bit
        >X-Newsreader: Microsoft CDO for Windows 2000
        >Thread-Index: AcOof2MGsd833c3 lTq29mA5i2VBUfg ==
        >X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
        >Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
        >Path: cpmsftngxa06.ph x.gbl
        >Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.framew ork.aspnet:1901 28
        >NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
        >X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
        >
        >the thing is I need to do it on the client side. I've
        >tried <% = jsTestServer %> before. It didn't work. It
        >didn't even pop up the alert this time. Or maybe I did
        >something wrong. Could you please give me a more
        >complete piece of code?
        >
        >Thanks!
        >[color=green]
        >>-----Original Message-----
        >>Jade,
        >>
        >>Assuming that variable is in scope, you would use the[/color]
        >following:[color=green]
        >>
        >><% = jsTestServer %>
        >>
        >>Actually, what you should do is use[/color]
        >RegisterClient ScriptBlock to generate[color=green]
        >>your client-side script on the server and then register[/color]
        >it on the client.[color=green]
        >>
        >>Jim Cheshire [MSFT]
        >>Developer Support
        >>ASP.NET
        >>jamesche@onli ne.microsoft.co m
        >>
        >>This post is provided as-is with no warranties and[/color]
        >confers no rights.[color=green]
        >>
        >>--------------------[color=darkred]
        >>>Content-Class: urn:content-classes:message
        >>>From: "Jade" <anonymous@disc ussions.microso ft.com>
        >>>Sender: "Jade" <anonymous@disc ussions.microso ft.com>
        >>>Subject: how to call aspx variable from Javascript
        >>>Date: Tue, 11 Nov 2003 09:30:27 -0800
        >>>Lines: 16
        >>>Message-ID: <0dd401c3a879$7 f220130$a001280 a@phx.gbl>
        >>>MIME-Version: 1.0
        >>>Content-Type: text/plain;
        >>> charset="iso-8859-1"
        >>>Content-Transfer-Encoding: 7bit
        >>>X-Newsreader: Microsoft CDO for Windows 2000
        >>>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
        >>>Thread-Index: AcOoeX8iZVlOZ5x 5TzC4ue9n01Hi8w ==
        >>>Newsgroups : microsoft.publi c.dotnet.framew ork.aspnet
        >>>Path: cpmsftngxa06.ph x.gbl
        >>>Xref: cpmsftngxa06.ph x.gbl[/color][/color]
        >microsoft.publ ic.dotnet.frame work.aspnet:190 114[color=green][color=darkred]
        >>>NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
        >>>X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
        >>>
        >>>I saw some web page saying I could do it this way in
        >>>javascript :
        >>>var iNumber = <%#publicvarnam e [or] publicpropertyn ame %[/color]
        >>[color=darkred]
        >>>
        >>>but it doesn't seem to work. I have this piece of code
        >>>here in javascript:
        >>><script language="javas cript">
        >>> var sessionServer = "<%#jsTestServe r%>";
        >>> alert(sessionSe rver);
        >>></script>
        >>>
        >>>and the variable is defined in the C# code behind:
        >>>public string jsTestServer
        >>>= "\\\\TestServer \\TestSession\\ ";
        >>>
        >>>when I run it, alert has no message in it.
        >>>[/color]
        >>
        >>.
        >>[/color]
        >[/color]

        Comment

        • Vaibhav

          #5
          Re: how to call aspx variable from Javascript

          make sure you do like this
          "<%=jsTestServe r%>"

          "Jade" <anonymous@disc ussions.microso ft.com> wrote in message
          news:0dd401c3a8 79$7f220130$a00 1280a@phx.gbl.. .[color=blue]
          > I saw some web page saying I could do it this way in
          > javascript:
          > var iNumber = <%#publicvarnam e [or] publicpropertyn ame %>
          >
          > but it doesn't seem to work. I have this piece of code
          > here in javascript:
          > <script language="javas cript">
          > var sessionServer = "<%#jsTestServe r%>";
          > alert(sessionSe rver);
          > </script>
          >
          > and the variable is defined in the C# code behind:
          > public string jsTestServer
          > = "\\\\TestServer \\TestSession\\ ";
          >
          > when I run it, alert has no message in it.[/color]


          Comment

          • Infant Newbie

            #6
            Re: how to call aspx variable from Javascript

            javascript is client side and your variable is server side......
            "Jade" <anonymous@disc ussions.microso ft.com> wrote in message
            news:02a801c3a8 7f$63068330$a30 1280a@phx.gbl.. .[color=blue]
            > the thing is I need to do it on the client side. I've
            > tried <% = jsTestServer %> before. It didn't work. It
            > didn't even pop up the alert this time. Or maybe I did
            > something wrong. Could you please give me a more
            > complete piece of code?
            >
            > Thanks!
            >[color=green]
            > >-----Original Message-----
            > >Jade,
            > >
            > >Assuming that variable is in scope, you would use the[/color]
            > following:[color=green]
            > >
            > ><% = jsTestServer %>
            > >
            > >Actually, what you should do is use[/color]
            > RegisterClientS criptBlock to generate[color=green]
            > >your client-side script on the server and then register[/color]
            > it on the client.[color=green]
            > >
            > >Jim Cheshire [MSFT]
            > >Developer Support
            > >ASP.NET
            > >jamesche@onlin e.microsoft.com
            > >
            > >This post is provided as-is with no warranties and[/color]
            > confers no rights.[color=green]
            > >
            > >--------------------[color=darkred]
            > >>Content-Class: urn:content-classes:message
            > >>From: "Jade" <anonymous@disc ussions.microso ft.com>
            > >>Sender: "Jade" <anonymous@disc ussions.microso ft.com>
            > >>Subject: how to call aspx variable from Javascript
            > >>Date: Tue, 11 Nov 2003 09:30:27 -0800
            > >>Lines: 16
            > >>Message-ID: <0dd401c3a879$7 f220130$a001280 a@phx.gbl>
            > >>MIME-Version: 1.0
            > >>Content-Type: text/plain;
            > >> charset="iso-8859-1"
            > >>Content-Transfer-Encoding: 7bit
            > >>X-Newsreader: Microsoft CDO for Windows 2000
            > >>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
            > >>Thread-Index: AcOoeX8iZVlOZ5x 5TzC4ue9n01Hi8w ==
            > >>Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
            > >>Path: cpmsftngxa06.ph x.gbl
            > >>Xref: cpmsftngxa06.ph x.gbl[/color][/color]
            > microsoft.publi c.dotnet.framew ork.aspnet:1901 14[color=green][color=darkred]
            > >>NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
            > >>X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
            > >>
            > >>I saw some web page saying I could do it this way in
            > >>javascript:
            > >>var iNumber = <%#publicvarnam e [or] publicpropertyn ame %[/color]
            > >[color=darkred]
            > >>
            > >>but it doesn't seem to work. I have this piece of code
            > >>here in javascript:
            > >><script language="javas cript">
            > >> var sessionServer = "<%#jsTestServe r%>";
            > >> alert(sessionSe rver);
            > >></script>
            > >>
            > >>and the variable is defined in the C# code behind:
            > >>public string jsTestServer
            > >>= "\\\\TestServer \\TestSession\\ ";
            > >>
            > >>when I run it, alert has no message in it.
            > >>[/color]
            > >
            > >.
            > >[/color][/color]


            Comment

            Working...