How to detect page refresh in .net

后端 未结 6 861
梦如初夏
梦如初夏 2020-12-31 03:55

I have a Button_click event. While refreshing the page the previous Postback event is triggering again. How do I identify the page refresh event to

6条回答
  •  借酒劲吻你
    2020-12-31 04:30

    using the viewstate worked a lot better for me as detailed here. Basically:

    bool IsPageRefresh = false;
    
    //this section of code checks if the page postback is due to genuine submit by user or by pressing "refresh"
    if (!IsPostBack)     
    {
        ViewState["ViewStateId"] = System.Guid.NewGuid().ToString();
        Session["SessionId"] = ViewState["ViewStateId"].ToString();
    }
    else
    {
        if (ViewState["ViewStateId"].ToString() != Session["SessionId"].ToString())
        {
            IsPageRefresh = true;
        }
    
        Session["SessionId"] = System.Guid.NewGuid().ToString();
        ViewState["ViewStateId"] = Session["SessionId"].ToString();
    }   
    

提交回复
热议问题