By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,627 Members | 1,149 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,627 IT Pros & Developers. It's quick & easy.

how to call aspx variable from Javascript

P: n/a
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.
Nov 15 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Jade" <an*******@discussions.microsoft.com> wrote in
news:12****************************@phx.gbl:
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,

Try <%= jsTestServer %> instead of <%# jsTestServer %>.
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #2

P: n/a
<%# is the DataBinding syntax. You need to make sure you call DataBind() on
your page (in the server code. You can also use <%=someVariable%>, which
will print the value out when it runs. The DataBind way is a little more
elegant, I feel.

-mike
MVP

"Jade" <an*******@discussions.microsoft.com> wrote in message
news:12****************************@phx.gbl...
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.

Nov 15 '05 #3

P: n/a
It still doesn't work. Here is exactly what I have now:

<script language="javascript">
var sessionServer = "<%=jsTestServer%>";
alert(sessionServer);

function CreateSessionFlag()
{
alert("hello");
}

function DeleteSessionFlag()
{
alert("bye");
}
</script>
</head>
<body MS_POSITIONING="GridLayout"
onLoad="CreateSessionFlag();">

In the C# code behind:
public class WebForm1 : System.Web.UI.Page
{
public string jsTestServer
= "\\\\ze483z\\TestSession\\";

private void Page_Load(object sender, System.EventArgs
e)
{ // Put user code to initialize the page here }

It's a very simple test program. After I changed to "<%
=jsTestServer%>", it generates some jscript error -
object expected on the "onload" line. If I used the old
way var sessionServer = "<%#jsTestServer%>"; it pops up
an alert with empty message.

-----Original Message-----
<%# is the DataBinding syntax. You need to make sure you call DataBind() onyour page (in the server code. You can also use <% =someVariable%>, whichwill print the value out when it runs. The DataBind way is a little moreelegant, I feel.

-mike
MVP

"Jade" <an*******@discussions.microsoft.com> wrote in messagenews:12****************************@phx.gbl...
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.

.

Nov 15 '05 #4

P: n/a
Check out HTML output. If you have \\ characters, they will get interpreted
by C#. Try with your variable as "Hello, JScript!" :).

Using the <%# syntax, did you do a this.DataBind(); call in your OnLoad or
somewhere?

-mike
MVP

<an*******@discussions.microsoft.com> wrote in message
news:14****************************@phx.gbl...
It still doesn't work. Here is exactly what I have now:

<script language="javascript">
var sessionServer = "<%=jsTestServer%>";
alert(sessionServer);

function CreateSessionFlag()
{
alert("hello");
}

function DeleteSessionFlag()
{
alert("bye");
}
</script>
</head>
<body MS_POSITIONING="GridLayout"
onLoad="CreateSessionFlag();">

In the C# code behind:
public class WebForm1 : System.Web.UI.Page
{
public string jsTestServer
= "\\\\ze483z\\TestSession\\";

private void Page_Load(object sender, System.EventArgs
e)
{ // Put user code to initialize the page here }

It's a very simple test program. After I changed to "<%
=jsTestServer%>", it generates some jscript error -
object expected on the "onload" line. If I used the old
way var sessionServer = "<%#jsTestServer%>"; it pops up
an alert with empty message.

-----Original Message-----
<%# is the DataBinding syntax. You need to make sure

you call DataBind() on
your page (in the server code. You can also use <%

=someVariable%>, which
will print the value out when it runs. The DataBind way

is a little more
elegant, I feel.

-mike
MVP

"Jade" <an*******@discussions.microsoft.com> wrote in

message
news:12****************************@phx.gbl...
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.

.

Nov 15 '05 #5

P: n/a
It did interpreted correctly in HTML:
var sessionServer = "\\ze483z\TestSession\";

where/how should I use "this.DataBind();" exactly in
Onload???

-----Original Message-----
Check out HTML output. If you have \\ characters, they will get interpretedby C#. Try with your variable as "Hello, JScript!" :).

Using the <%# syntax, did you do a this.DataBind(); call in your OnLoad orsomewhere?

-mike
MVP

<an*******@discussions.microsoft.com> wrote in message
news:14****************************@phx.gbl...
It still doesn't work. Here is exactly what I have now:
<script language="javascript">
var sessionServer = "<%=jsTestServer%>";
alert(sessionServer);

function CreateSessionFlag()
{
alert("hello");
}

function DeleteSessionFlag()
{
alert("bye");
}
</script>
</head>
<body MS_POSITIONING="GridLayout"
onLoad="CreateSessionFlag();">

In the C# code behind:
public class WebForm1 : System.Web.UI.Page
{
public string jsTestServer
= "\\\\ze483z\\TestSession\\";

private void Page_Load(object sender, System.EventArgs e)
{ // Put user code to initialize the page here }

It's a very simple test program. After I changed to "<% =jsTestServer%>", it generates some jscript error -
object expected on the "onload" line. If I used the old way var sessionServer = "<%#jsTestServer%>"; it pops up
an alert with empty message.

>-----Original Message-----
><%# is the DataBinding syntax. You need to make sure

you call DataBind() on
>your page (in the server code. You can also use <%

=someVariable%>, which
>will print the value out when it runs. The DataBind way
is a little more
>elegant, I feel.
>
>-mike
>MVP
>
>"Jade" <an*******@discussions.microsoft.com> wrote in

message
>news:12****************************@phx.gbl...
>> 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.
>
>
>.
>

.

Nov 15 '05 #6

P: n/a
> It did interpreted correctly in HTML:
var sessionServer = "\\ze483z\TestSession\";
Right, and that's not valid for JScript, thus the runtime error.
where/how should I use "this.DataBind();" exactly in
Onload???
this.DataBind(); // That's it

That binds all the <%# markers.

-mike
MVP

-----Original Message-----
Check out HTML output. If you have \\ characters, they

will get interpreted
by C#. Try with your variable as "Hello, JScript!" :).

Using the <%# syntax, did you do a this.DataBind(); call

in your OnLoad or
somewhere?

-mike
MVP

<an*******@discussions.microsoft.com> wrote in message
news:14****************************@phx.gbl...
It still doesn't work. Here is exactly what I have now:
<script language="javascript">
var sessionServer = "<%=jsTestServer%>";
alert(sessionServer);

function CreateSessionFlag()
{
alert("hello");
}

function DeleteSessionFlag()
{
alert("bye");
}
</script>
</head>
<body MS_POSITIONING="GridLayout"
onLoad="CreateSessionFlag();">

In the C# code behind:
public class WebForm1 : System.Web.UI.Page
{
public string jsTestServer
= "\\\\ze483z\\TestSession\\";

private void Page_Load(object sender, System.EventArgs e)
{ // Put user code to initialize the page here }

It's a very simple test program. After I changed to "<% =jsTestServer%>", it generates some jscript error -
object expected on the "onload" line. If I used the old way var sessionServer = "<%#jsTestServer%>"; it pops up
an alert with empty message.
>-----Original Message-----
><%# is the DataBinding syntax. You need to make sure
you call DataBind() on
>your page (in the server code. You can also use <%
=someVariable%>, which
>will print the value out when it runs. The DataBind way is a little more
>elegant, I feel.
>
>-mike
>MVP
>
>"Jade" <an*******@discussions.microsoft.com> wrote in
message
>news:12****************************@phx.gbl...
>> 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.
>
>
>.
>

.

Nov 15 '05 #7

P: n/a
I'm sorry, Mike. I still don't know where to
put "this.DataBind();" I put it in
private void Page_Load(object sender, System.EventArgs e)
{
this.DataBind();
}

and I still have the runtime error.

-----Original Message-----
It did interpreted correctly in HTML:
var sessionServer = "\\ze483z\TestSession\";
Right, and that's not valid for JScript, thus the

runtime error.
where/how should I use "this.DataBind();" exactly in
Onload???


this.DataBind(); // That's it

That binds all the <%# markers.

-mike
MVP

>-----Original Message-----
>Check out HTML output. If you have \\ characters, they
will get interpreted
>by C#. Try with your variable as "Hello,
JScript!" :). >
>Using the <%# syntax, did you do a this.DataBind(); call in your OnLoad or
>somewhere?
>
>-mike
>MVP
>
><an*******@discussions.microsoft.com> wrote in message
>news:14****************************@phx.gbl...
>> It still doesn't work. Here is exactly what I have

now:
>>
>> <script language="javascript">
>> var sessionServer = "<%=jsTestServer%>";
>> alert(sessionServer);
>>
>> function CreateSessionFlag()
>> {
>> alert("hello");
>> }
>>
>> function DeleteSessionFlag()
>> {
>> alert("bye");
>> }
>> </script>
>> </head>
>> <body MS_POSITIONING="GridLayout"
>> onLoad="CreateSessionFlag();">
>>
>> In the C# code behind:
>> public class WebForm1 : System.Web.UI.Page
>> {
>> public string jsTestServer
>> = "\\\\ze483z\\TestSession\\";
>>
>> private void Page_Load(object sender,

System.EventArgs
>> e)
>> { // Put user code to initialize the page here }
>>
>> It's a very simple test program. After I changed

to "<%
>> =jsTestServer%>", it generates some jscript error -
>> object expected on the "onload" line. If I used the

old
>> way var sessionServer = "<%#jsTestServer%>"; it
pops up >> an alert with empty message.
>>
>>
>> >-----Original Message-----
>> ><%# is the DataBinding syntax. You need to make sure >> you call DataBind() on
>> >your page (in the server code. You can also use <%
>> =someVariable%>, which
>> >will print the value out when it runs. The DataBind way
>> is a little more
>> >elegant, I feel.
>> >
>> >-mike
>> >MVP
>> >
>> >"Jade" <an*******@discussions.microsoft.com> wrote
in >> message
>> >news:12****************************@phx.gbl...
>> >> 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.
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Nov 15 '05 #8

P: n/a
The runtime error is from JScript most likely.

var sessionServer = "\\ze483z\TestSession\"; is not a valid string literal.

-mike
MVP

"Jade" <an*******@discussions.microsoft.com> wrote in message
news:03****************************@phx.gbl...
I'm sorry, Mike. I still don't know where to
put "this.DataBind();" I put it in
private void Page_Load(object sender, System.EventArgs e)
{
this.DataBind();
}

and I still have the runtime error.

-----Original Message-----
It did interpreted correctly in HTML:
var sessionServer = "\\ze483z\TestSession\";


Right, and that's not valid for JScript, thus the

runtime error.
where/how should I use "this.DataBind();" exactly in
Onload???


this.DataBind(); // That's it

That binds all the <%# markers.

-mike
MVP


>-----Original Message-----
>Check out HTML output. If you have \\ characters, they will get interpreted
>by C#. Try with your variable as "Hello, JScript!" :). >
>Using the <%# syntax, did you do a this.DataBind(); call in your OnLoad or
>somewhere?
>
>-mike
>MVP
>
><an*******@discussions.microsoft.com> wrote in message
>news:14****************************@phx.gbl...
>> It still doesn't work. Here is exactly what I have
now:
>>
>> <script language="javascript">
>> var sessionServer = "<%=jsTestServer%>";
>> alert(sessionServer);
>>
>> function CreateSessionFlag()
>> {
>> alert("hello");
>> }
>>
>> function DeleteSessionFlag()
>> {
>> alert("bye");
>> }
>> </script>
>> </head>
>> <body MS_POSITIONING="GridLayout"
>> onLoad="CreateSessionFlag();">
>>
>> In the C# code behind:
>> public class WebForm1 : System.Web.UI.Page
>> {
>> public string jsTestServer
>> = "\\\\ze483z\\TestSession\\";
>>
>> private void Page_Load(object sender,
System.EventArgs
>> e)
>> { // Put user code to initialize the page here }
>>
>> It's a very simple test program. After I changed
to "<%
>> =jsTestServer%>", it generates some jscript error -
>> object expected on the "onload" line. If I used the
old
>> way var sessionServer = "<%#jsTestServer%>"; it pops up >> an alert with empty message.
>>
>>
>> >-----Original Message-----
>> ><%# is the DataBinding syntax. You need to make sure >> you call DataBind() on
>> >your page (in the server code. You can also use <%
>> =someVariable%>, which
>> >will print the value out when it runs. The DataBind way
>> is a little more
>> >elegant, I feel.
>> >
>> >-mike
>> >MVP
>> >
>> >"Jade" <an*******@discussions.microsoft.com> wrote in >> message
>> >news:12****************************@phx.gbl...
>> >> 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.
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.