简体   繁体   中英

Get public property in javascript after ASP.NET partial postback

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
}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM