Friday, January 5, 2018

Disable form repost on browser refresh

Here's a trick I found to prevent a form repost on browser refresh


        protected void Page_PreRender(object sender, EventArgs e)
        {
                     ViewState["Check_Page_Refresh"] = Session["Check_Page_Refresh"];
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["Check_Page_Refresh"] = DateTime.Now.ToString();
            }
        }

protected void btn_Click(object sender, EventArgs e)
{
if (ViewState["Check_Page_Refresh"].ToString() == Session["Check_Page_Refresh"].ToString())
{
     <your main code you want to execute once on button click here>
}
}

No comments:

Post a Comment

Thank you for your comment.