gpt4 book ai didi

javascript - 返回并重新加载页面时显示不需要的服务器端警报消息

转载 作者:行者123 更新时间:2023-11-27 23:09:09 25 4
gpt4 key购买 nike

我有一个提交按钮,下面是 onClick 事件中的代码:

protected void btnSubmit_Click(object sender, EventArgs e)
{
...
ScriptManager.RegisterClientScriptBlock(this, GetType(), "alertMessage", "alert('Submitted')", true);
}

这段代码有效。

但问题是当用户通过以下方式转到下一页时:

Response.Redirect("page2.aspx");

当点击退格键返回到 page1 时,

重新加载之前,

出现以下消息框!!

this problem happened again when we refresh(F5) the page1 after submiting

我该如何解决这个问题?

我尝试过:

 1. if(isPostback)// before the alert
2. string message = "Submitted";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());

最佳答案

在这种情况下,您可以实现一个特殊的代码块来检测浏览器刷新

private bool refreshState;
private bool isRefresh;

protected override void LoadViewState(object savedState)
{
object[] AllStates = (object[])savedState;
base.LoadViewState(AllStates[0]);
refreshState = bool.Parse(AllStates[1].ToString());
if (Session["ISREFRESH"] != null && Session["ISREFRESH"] != "")
isRefresh = (refreshState == (bool)Session["ISREFRESH"]);
}

protected override object SaveViewState()
{
Session["ISREFRESH"] = refreshState;
object[] AllStates = new object[3];
AllStates[0] = base.SaveViewState();
AllStates[1] = !(refreshState);
return AllStates;
}

在按钮提交中,您可以这样做

protected void btn3_Click(object sender, EventArgs e)
{
if (isRefresh == false)
{
Insert Code here
}
}

关于javascript - 返回并重新加载页面时显示不需要的服务器端警报消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36330733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com