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

时间:2016-03-31 10:25:10

标签: javascript html asp.net alert

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

function makeAppState(foo, bar, baz) {
  return {
    qux: foo + bar - baz,
    update() {
      this.qux = 0;
    }
  };
}

此代码有效。

但问题是当用户通过此页面转到下一页时:

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

并单击退格键返回page1和

重新加载之前,

出现以下消息框!!

Response.Redirect("page2.aspx");

我将如何解决这个问题?

我试过了:

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

1 个答案:

答案 0 :(得分:1)

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

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