[英]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. 我试图通过javascript来获取我的asp.net代码的公共属性的值,如果这是页面的首次加载,它似乎可以正常工作。 However, on subsequent partial postbacks, the value I am able to access via javascript is still what it was on the initial page load. 但是,在随后的部分回发中,我能够通过javascript访问的值仍然是初始页面加载时的值。 The javascript code I have is: 我拥有的javascript代码是:
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. 这是因为<%= %>
等效于Response.Write
,它仅在初始加载和完整回发时才能正常工作。 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.