我需要运行 JavaScript 函数 onLoad(),但仅在页面第一次加载时才执行(即不是回发的结果)。
基本上,我需要检查 JavaScript 中的 IsPostBack。
谢谢。
服务器端,写:
if(IsPostBack)
{
// NOTE: the following uses an overload of RegisterClientScriptBlock()
// that will surround our string with the needed script tags
ClientScript.RegisterClientScriptBlock(GetType(), "IsPostBack", "var isPostBack = true;", true);
}
然后,在为 onLoad 运行的脚本中,检查该变量是否存在:
if(isPostBack) {
// do your thing
}
否则,您实际上不需要设置变量,就像乔纳森的解决方案一样。客户端 if 语句将正常工作,因为“isPostBack”变量将是未定义的,该变量在该 if 语句中计算为 false。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)