我需要运行一个 JavaScript 函数 onLoad(),但只有在页面第一次加载时才执行(即不是回发的结果)。
基本上,我需要在 JavaScript 中检查 IsPostBack。
谢谢你。
我需要运行一个 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。
有一种更简单的方法,不需要在后面的代码中编写任何内容:只需将此行添加到您的 javascript 中:
if(<%=(Not Page.IsPostBack).ToString().ToLower()%>){//Your JavaScript goodies here}
或者
if(<%=(Page.IsPostBack).ToString().ToLower()%>){//Your JavaScript goodies here}
嗨,请尝试以下...
function pageLoad (sender, args) {
alert (args._isPartialLoad);
}
结果是一个布尔值
该解决方案对我不起作用,我不得不对其进行调整:
protected void Page_Load(object sender, EventArgs e)
{
string script;
if (IsPostBack)
{
script = "var isPostBack = true;";
}
else
{
script = "var isPostBack = false;";
}
Page.ClientScript.RegisterStartupScript(GetType(), "IsPostBack", script, true);
}
希望这可以帮助。
您可以在页面上放置一个隐藏的输入,并在页面加载后为其赋值。然后您可以检查该字段,如果它在发布数据中,则是回发,否则不是。
有两种使用服务器端代码(ASP.NET 特定)作为响应发布的解决方案。我认为值得指出的是,该解决方案与技术无关,因为它仅使用客户端功能,这些功能在所有主要浏览器中都可用。