Do you mean that you need your javascript on hte page to have access to the variable?
Because otherwise, when you post back the page, the variable will be available...the only time your webform would need to access it seperate from the code behind is through client script.
Maybe give us more info on what you're trying to do and we can go from there.
You can emit client side javascript code to the browser in many different ways.
Arguably the best way to do this is to use the .RegisterStartupScript method of the Page object. For example, let's say that I have a variable, myServerVar in codebehind that I want accessible to a client side javascript function. First, you need to build the client side code that will be sent to the browser:
//this is in your codebehind
int myServerVar = 7;
string script = "<script language=javascript>var myClientVar = " + myVar.ToString() + ";</script>";
Now, you need to invoke the RegisterStartupScript method to send it to the browser:
Page.RegisterStartupScript("clientCode", script);
The net result is that this bit of js code will be emitted to the browser at the bottom of the page... right before the closing </form> tag. As you can see from the javascript, the client side variable, myClientVar, has been declared as a global variable, and as such, will be accessible in any function that you then choose to declare on the page.
If you need to control where on the page the js appears, then you can place an <asp:Label /> on the page, and then just assign the script value to the .Text property of the label. Most times, however, Page.RegisterStartupScript() is more than sufficient.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.