Friday, February 29, 2008

Handling page refresh (F5) in ASP.NET post backs

Handling page refresh (F5) on post backs is one of the biggest problem for many web applications.Usually we used the simplest solution by redirecting to the same page after the action, but not always usefull.

In the previous post we saw how to handle a multiple user clicks on the same button.
but still user can overdo things by page refresh (F5).

I use a header user control in all my applications which I found is the best place to handle this. The theory is F5 will post a older viewstate so if we have anything to compare we can skip the action from being perfomed once again.

The below code in .NET2.0 goes well with IE.
Insert it in your page or some common header/banner control


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["PostID"] = "1001";
ViewState["PostID"] = Session["PostID"].ToString();
}
}

public bool IsValidPost()
{
if (ViewState["PostID"].ToString()
== Session["PostID"].ToString())
{
Session["PostID"] =
(Convert.ToInt16(Session["PostID"]) + 1).ToString();

ViewState["PostID"] = Session["PostID"].ToString();

return true;
}
else
{
ViewState["PostID"] =
Session["PostID"].ToString();

return false;
}

}



Now in your button click verify if the postback is real button click


public void ActionEvent(object sender, CommandEventArgs e)
{
if (IsValidPost())
{
DoPocessing();
}
}

15 comments:

sadia said...

nice one
thanks

Anonymous said...

Fantastic!
Many thanks!
Mark

Pawan Chandak said...

Thanks! I got exactly what I was looking for.. and now I have implemented it everywhere on my webpage

Al Stepone said...

Great solution. Easy to implement. I like it! Thanks a lot.

Anonymous said...

Hi really nice post.even i am facing the same problem.really nice post.thanks a lot Prashant

Anonymous said...

Nice work.
But this solution will be only suitable if one page has one button. what if there are multiple button.

laxman said...

You rock.

Arash said...

It is amazing. Thanks

Consultoria RH said...

Este blog é uma representação exata de competências. Eu gosto da sua recomendação. Um grande conceito que reflete os pensamentos do escritor.Consultoria RH

Andres said...

Increíble!!
Muchas gracias por el código, funciono exactamente como lo necesitaba

Usman Khalid said...

O Great man! You saved my last remaining hairs. :P

Anonymous said...

simplest approach take a hidden variable and sets its value after ur event fires from client side then chck the same value at ur button click on ur server side code if its same then do processing and at the end simply reset variable agin when person refersh page value is reset ur button click functionality never occurs till it sets by client side click.:)

vinayaka a said...

Hey Prashant,

I pay you respect. You are the man of situation. Thanks for such an awesome trick for kicking the ass of asp.net refresh issue :)

Regards,
Vinayaka

chirag said...

It is not working

Anonymous said...

It worked on local machine but stucked me after uploading page. I have done this in a different way. see this http://stackoverflow.com/questions/21255178/prevent-page-re-submission-after-refresh-asp-net