Server Variable in Client Script

B

Bob T

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>
 
C

Curt_C [MVP]

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.
 
S

S. Justin Gengo

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
 
S

S. Justin Gengo

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
 
B

Bob T

Thanks all

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

Bob T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top