Get public property in javascript after ASP.NET partial postback

431 views Asked by At

I'm attempting to grab the value of a public property of my asp.net code behind via javascript, which seems to work fine if it's the first time the page loads. However, on subsequent partial postbacks, the value I am able to access via javascript is still what it was on the initial page load. The javascript code I have is:

function pageLoad(sender, args) {
    var foo = '<%= Foo %>';
    //value of foo never changes even though it is changing in code behind
}
1

There are 1 answers

0
Yuriy Galanter On

That's because <%= %> is an equivalent of Response.Write which works correctly only on initial loads and full postbacks. In you case initial value becomes hard-coded value for the function (which you can see by doing a View Source of the page).

A better approach to exchange values with client-side code would be a hidden field. Set its value in server side and read it back in client:

function pageLoad(sender, args) {
    var foo = $get('hiddenFieldID').value;
}