how to call aspx variable from Javascript

  • Thread starter Thread starter Jade
  • Start date Start date
J

Jade

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

but it doesn't seem to work. I have this piece of code
here in javascript:
<script language="javascript">
var sessionServer = "<%#jsTestServer%>";
alert(sessionServer);
</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.
 
Jade,

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

<% = jsTestServer %>

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

Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

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

--------------------
 
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!
-----Original Message-----
Jade,

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

<% = jsTestServer %>

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

Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

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

--------------------
Content-Class: urn:content-classes:message
From: "Jade" <[email protected]>
Sender: "Jade" <[email protected]>
Subject: how to call aspx variable from Javascript
Date: Tue, 11 Nov 2003 09:30:27 -0800
Lines: 16
Message-ID: <[email protected]>
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: AcOoeX8iZVlOZ5x5TzC4ue9n01Hi8w==
Newsgroups: microsoft.public.dotnet.framework.aspnet
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:190114
NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

I saw some web page saying I could do it this way in
javascript:
var iNumber = <%#publicvarname [or] publicpropertyname %
but it doesn't seem to work. I have this piece of code
here in javascript:
<script language="javascript">
var sessionServer = "<%#jsTestServer%>";
alert(sessionServer);
</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.

.
 
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 RegisterClientScriptBlock to create the client-side code on the
server and then write it to the page.

Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

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

--------------------
Content-Class: urn:content-classes:message
From: "Jade" <[email protected]>
Sender: "Jade" <[email protected]>
References: <[email protected]>
Subject: RE: how to call aspx variable from Javascript
Date: Tue, 11 Nov 2003 10:12:37 -0800
Lines: 72
Message-ID: <[email protected]>
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: AcOof2MGsd833c3lTq29mA5i2VBUfg==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.public.dotnet.framework.aspnet
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:190128
NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
X-Tomcat-NG: microsoft.public.dotnet.framework.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!
-----Original Message-----
Jade,

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

<% = jsTestServer %>

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

Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

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

--------------------
Content-Class: urn:content-classes:message
From: "Jade" <[email protected]>
Sender: "Jade" <[email protected]>
Subject: how to call aspx variable from Javascript
Date: Tue, 11 Nov 2003 09:30:27 -0800
Lines: 16
Message-ID: <[email protected]>
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: AcOoeX8iZVlOZ5x5TzC4ue9n01Hi8w==
Newsgroups: microsoft.public.dotnet.framework.aspnet
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:190114
NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

I saw some web page saying I could do it this way in
javascript:
var iNumber = <%#publicvarname [or] publicpropertyname %
but it doesn't seem to work. I have this piece of code
here in javascript:
<script language="javascript">
var sessionServer = "<%#jsTestServer%>";
alert(sessionServer);
</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.

.
 
javascript is client side and your variable is server side......
Jade said:
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!
-----Original Message-----
Jade,

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

<% = jsTestServer %>

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

Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

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

--------------------
Content-Class: urn:content-classes:message
From: "Jade" <[email protected]>
Sender: "Jade" <[email protected]>
Subject: how to call aspx variable from Javascript
Date: Tue, 11 Nov 2003 09:30:27 -0800
Lines: 16
Message-ID: <[email protected]>
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: AcOoeX8iZVlOZ5x5TzC4ue9n01Hi8w==
Newsgroups: microsoft.public.dotnet.framework.aspnet
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:190114
NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

I saw some web page saying I could do it this way in
javascript:
var iNumber = <%#publicvarname [or] publicpropertyname %
but it doesn't seem to work. I have this piece of code
here in javascript:
<script language="javascript">
var sessionServer = "<%#jsTestServer%>";
alert(sessionServer);
</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.

.
 
Back
Top