How to avoid the button events on Refresh of page

前端 未结 4 1116
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-09 16:45

I have .aspx page that page inserts the data to the database on a button click. But when i press the button it is going right. i m getting the Successfully message as \" success

4条回答
  •  庸人自扰
    2021-02-09 17:31

    Add this in your class:

    #region Browser Refresh
    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;
    }
    
    #endregion 
    

    And in your button click do this:

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

提交回复
热议问题