Solved

javascript variable on code behind

Posted on 2008-06-26
4
1,647 Views
Last Modified: 2008-11-27
I have a variable named 'timeoutlocation' defined in an external JS file.
I need to assign a different value to this 'timeoutlocation' variable on code behind page.

How can I access a external JS variable on .aspx.cs page??
Would appreciate any help in the code.
0
Comment
Question by:newaspdotnet
4 Comments
 
LVL 10

Accepted Solution

by:
Dxpert earned 500 total points
ID: 21878653
Get that variable out of the external JS and place it on the ASPX page (front end). Then declare a public variable on the code behind page then set its value on Page_Load.

On the ASPX page do this use <%=%> to print the value from the public variable set on the code behind.

pseudo code:

javascript code...

var timeoutlocation = <%=NewVariableFromCodeBehide%>

end javascript code...


0
 
LVL 16

Expert Comment

by:ororiole
ID: 21878715
You have to include your external js file on the page obviously. Then you define a hidden field on your page, place the value in it from the code behind.

Inside a js function get the hidden field and set your variable to its value. Tog et an aspx control use the following syntax:

function SetTimeoutLocation()
{
   timeoutlocation = document.GetElementById('<%=hiddenField1.ClientID%>').value;
}
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question