aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Detect Browser Refresh to avoid events fired again in ASP.NET

Apr 6, 2010

If you have created an aspx page using C# and ASP.NET and have put a button on it. And in the Click event of this button if you are inserting some data in database , after click if user refresh the page than click event gets fired again resulting data insertion to database again, to stop events on the page getting fired on browser refresh we need to write bit of code to avoid it.

In this example I’ve put a Label and a Button on the page, on click the label Text becomes Hello and when I refresh the page label's text again becomes Hello


<%@ Page Language="C#" AutoEventWireup="true"CodeFile="Default.aspx.cs" Inherits="_Default" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
<form id="form2" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label">asp:Label><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />div>

Now in the Page_Load event I m creating a Session Variable and assigning System date and time to it , and in Page_Prerender event I am creating a Viewstate variable and assigning Session variable's value to it than in button's click event I am checking the values of Session variable and Viewstate variable if they both are equal than page is not refreshed otherwise it has been refreshed 

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
Session["CheckRefresh"] =
protected void imgbtn_Click(object sender, ImageClickEventArgs e)
//Label2.Text = imgbtn.ToolTip;
protected void Button1_Click(object sender, EventArgs e)
if (Session["CheckRefresh"].ToString() == ViewState["CheckRefresh"].ToString())
Label1.Text = "Hello";
Session["CheckRefresh"] =
Label1.Text = "Page Refreshed";
protected void Page_PreRender(object sender, EventArgs e)
ViewState["CheckRefresh"] = Session["CheckRefresh"];

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


test said...

Super...Great Work!!!

Anto Reegan said...

i want redirect when page get refreshing what should i do...but i also try responce.redirect mehod but it wont work..plz help me..

krishna g said...
This comment has been removed by the author.
Anonymous said...

i got this error for session object...plz help

Object reference not set to an instance of an object.

thanks in adv

Anonymous said...

Thanks! It worked for me, but in my case, i've used

Anonymous said...

Thanks for this simple and elegant solution!

Nams said...

Great work!!! Is this really coded by you or you have taken it from codeproject's article which is written by Namita Patil? :)))

Akhilesh Chintu said...

Good one..!!

Pandiyan M said...

Thank you Suresh Bro,

Pandiyan M said...

No words to say but Am Happy

adi r said...

thanks it's work for me.

Manisha said...

Very good article ! It helped me on time.Thank you :)

Anonymous said...

Good Work-Around!

But will it work if same webpage (or another webpage from same application) is opened in multiple tabs??

Because, Session variable is used here, which may change its value in each tab and a last opened tab will assigned final value to session variable.

And i think In this scenario, Only last tab will work fine and previous tabs will not.

Also, i am not sure if this technique will work for multiple submit buttons on same web-page.

Please suggest your thoughts on this.

Anonymous said...

not work for me in C#

Anonymous said...

how to set this in MODALPOPUP

Siddiqui Anas said...

Thanks man!
I was wandering here and there for this topic. A simple yet amazing workaround!!
Thanks a ton!!

Anonymous said...

Thank you very much..
ASPDOT NET Suresh is the best compare to other websites of

Santosh said...

Hi Sir your articles are very nice.

Santosh said...
This comment has been removed by the author.
Manasa Sj said...

i got object reference error please tell me solution

Give your Valuable Comments

Other Related Posts

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.