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

Server Variable in Client Script

P: n/a
Hi All,

I am trying to pass a variable from my VB asp.net script (from for example
Sub Page_Load in mypage.aspx.vb) to my Client side script.

I have found and looked at a very good example "Client and Server Scripting
in Web Pages" but it only shows server side scripting that is written in
HTML in mypage.aspx and not script from mypage.aspx.vb. How can I pass a
variable value from mypage.aspx.vp to my client side script?
Sample from "Client and Server Scripting in Web Pages"

<%@ LANGUAGE="VBSCRIPT" %>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual InterDev 1.0">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>

<% Dim strMyServerVar
strMyServerVar = 42 %>
.........

The following line of HTML was generated on the client to print the value of
a client variable into the page after the page was sent to the browser. The
value of the client variable has been set equal to the value of the server
variable printed above.
<BR>
<BR>
<SCRIPT LANGUAGE = "JavaScript">
<!---
var x ;
x = <%= strMyServerVar %> ;
if (x == <%= strMyServerVar%>) {
document.write ("<STRONG>The value of the client variable is " + x +
".</STRONG>")
}
//
--->
</SCRIPT>
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Few ways... build your clientside script dynamically from within your server
side script, passing in the values.
Also, could always put the values in a hidden text box and have the
clientside pull from that.

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
"Bob T" <tw*****@hotmail.com> wrote in message
news:e9**************@TK2MSFTNGP09.phx.gbl...
Hi All,

I am trying to pass a variable from my VB asp.net script (from for example
Sub Page_Load in mypage.aspx.vb) to my Client side script.

I have found and looked at a very good example "Client and Server Scripting in Web Pages" but it only shows server side scripting that is written in
HTML in mypage.aspx and not script from mypage.aspx.vb. How can I pass a
variable value from mypage.aspx.vp to my client side script?
Sample from "Client and Server Scripting in Web Pages"

<%@ LANGUAGE="VBSCRIPT" %>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual InterDev 1.0">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>

<% Dim strMyServerVar
strMyServerVar = 42 %>
........

The following line of HTML was generated on the client to print the value of a client variable into the page after the page was sent to the browser. The value of the client variable has been set equal to the value of the server
variable printed above.
<BR>
<BR>
<SCRIPT LANGUAGE = "JavaScript">
<!---
var x ;
x = <%= strMyServerVar %> ;
if (x == <%= strMyServerVar%>) {
document.write ("<STRONG>The value of the client variable is " + x +
".</STRONG>")
}
//
--->
</SCRIPT>

Nov 18 '05 #2

P: n/a
Bob,

Why would you do this?

The whole point of using a code behind page is so that the programming logic
can be separated from the display. Placing the programming logic in both
places defeats one of the best things about .NET.

Just Dim your variable in the code behind and you're done.

Move this line to the code behind page like this:

Public Class WebForm1
Inherits System.Web.UI.Page
Public strMyServerVar As String = "42"
'---Rest of page code below
And remove the variable declaration from the .aspx page altogether:
Delete This: <% Dim strMyServerVar strMyServerVar = 42 %>

Your code will still work the same way. The .aspx page will have access to
the variable because it's declared as Public (you could also declare it as
Protected or Protected Friend)

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Bob T" <tw*****@hotmail.com> wrote in message
news:e9**************@TK2MSFTNGP09.phx.gbl...
Hi All,

I am trying to pass a variable from my VB asp.net script (from for example
Sub Page_Load in mypage.aspx.vb) to my Client side script.

I have found and looked at a very good example "Client and Server Scripting in Web Pages" but it only shows server side scripting that is written in
HTML in mypage.aspx and not script from mypage.aspx.vb. How can I pass a
variable value from mypage.aspx.vp to my client side script?
Sample from "Client and Server Scripting in Web Pages"

<%@ LANGUAGE="VBSCRIPT" %>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual InterDev 1.0">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>

<% Dim strMyServerVar
strMyServerVar = 42 %>
........

The following line of HTML was generated on the client to print the value of a client variable into the page after the page was sent to the browser. The value of the client variable has been set equal to the value of the server
variable printed above.
<BR>
<BR>
<SCRIPT LANGUAGE = "JavaScript">
<!---
var x ;
x = <%= strMyServerVar %> ;
if (x == <%= strMyServerVar%>) {
document.write ("<STRONG>The value of the client variable is " + x +
".</STRONG>")
}
//
--->
</SCRIPT>

Nov 18 '05 #3

P: n/a
Bob,

Just a quick note. My previous message shows how to do what you want, but I
shouldn't have included the "Why would you do this?" at the top of the
message.

I misunderstood part of your question.

Still, the code I provided does what you need.

Happy programming!

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Bob T" <tw*****@hotmail.com> wrote in message
news:e9**************@TK2MSFTNGP09.phx.gbl...
Hi All,

I am trying to pass a variable from my VB asp.net script (from for example
Sub Page_Load in mypage.aspx.vb) to my Client side script.

I have found and looked at a very good example "Client and Server Scripting in Web Pages" but it only shows server side scripting that is written in
HTML in mypage.aspx and not script from mypage.aspx.vb. How can I pass a
variable value from mypage.aspx.vp to my client side script?
Sample from "Client and Server Scripting in Web Pages"

<%@ LANGUAGE="VBSCRIPT" %>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual InterDev 1.0">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>

<% Dim strMyServerVar
strMyServerVar = 42 %>
........

The following line of HTML was generated on the client to print the value of a client variable into the page after the page was sent to the browser. The value of the client variable has been set equal to the value of the server
variable printed above.
<BR>
<BR>
<SCRIPT LANGUAGE = "JavaScript">
<!---
var x ;
x = <%= strMyServerVar %> ;
if (x == <%= strMyServerVar%>) {
document.write ("<STRONG>The value of the client variable is " + x +
".</STRONG>")
}
//
--->
</SCRIPT>

Nov 18 '05 #4

P: n/a
Thanks all

The rec from S. Justin Gengo works fine. I felt there was a simple way.

Bob T
"Bob T" <tw*****@hotmail.com> wrote in message
news:e9**************@TK2MSFTNGP09.phx.gbl...
Hi All,

I am trying to pass a variable from my VB asp.net script (from for example
Sub Page_Load in mypage.aspx.vb) to my Client side script.

I have found and looked at a very good example "Client and Server Scripting in Web Pages" but it only shows server side scripting that is written in
HTML in mypage.aspx and not script from mypage.aspx.vb. How can I pass a
variable value from mypage.aspx.vp to my client side script?
Sample from "Client and Server Scripting in Web Pages"

<%@ LANGUAGE="VBSCRIPT" %>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual InterDev 1.0">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>

<% Dim strMyServerVar
strMyServerVar = 42 %>
........

The following line of HTML was generated on the client to print the value of a client variable into the page after the page was sent to the browser. The value of the client variable has been set equal to the value of the server
variable printed above.
<BR>
<BR>
<SCRIPT LANGUAGE = "JavaScript">
<!---
var x ;
x = <%= strMyServerVar %> ;
if (x == <%= strMyServerVar%>) {
document.write ("<STRONG>The value of the client variable is " + x +
".</STRONG>")
}
//
--->
</SCRIPT>

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.