Detect Browser Refresh Avoid Events Fire In ASP.NET

Detect Browser Refresh In Asp.Net 2.0,3.5,4.0 To Avoid Events Getting Fired Or Event Firing,If you are inserting some data in database in Click event of button, After click if user refresh the page than click event gets fired again resulting data insertion to database again.

To stop event fire on browser refresh we need to write bit of code.

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

HTML SOURCE
   1:  <asp:Label ID="Label1" runat="server" Text=""/>
   2:   
   3:  <asp:Button ID="Button1" runat="server" 
   4:              OnClick="Button1_Click" Text="Button"/>


In 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

C# CODE
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["CheckRefresh"] =
            Server.UrlDecode(System.DateTime.Now.ToString());
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["CheckRefresh"].ToString() == ViewState["CheckRefresh"].ToString())
        {
            Label1.Text = "Hello";
            Session["CheckRefresh"] =
            Server.UrlDecode(System.DateTime.Now.ToString());
        }
        else
        {
            Label1.Text = "Page Refreshed";
        }
    }

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

VB.NET
Protected Sub Page_Load(sender As Object, e As EventArgs)
 If Not IsPostBack Then
  Session("CheckRefresh") = Server.UrlDecode(System.DateTime.Now.ToString())
 End If
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs)
 If Session("CheckRefresh").ToString() = ViewState("CheckRefresh").ToString() Then
  Label1.Text = "Hello"
  Session("CheckRefresh") = Server.UrlDecode(System.DateTime.Now.ToString())
 Else
  Label1.Text = "Page Refreshed"
 End If
End Sub

Protected Sub Page_PreRender(sender As Object, e As EventArgs)
 ViewState("CheckRefresh") = Session("CheckRefresh")
End Sub


If you like this post than join us or share

12 comments:

Anonymous said...

Thanks .worked Smoothly


Anonymous said...

yhanks for the nice code work perfectly,
i am just wondering is possible to change handler from refreshe to back button browser , please tell me how ?


Anonymous said...

The code is working fine. Can you explain how it works?

Because when the page prerender event occurs the value of session variable is copied to viewstate. When the page is refreshed in the browser how the value in viewstate variable is less than value in the session variable


Anonymous said...

pls explain how this works ..i cant able to understand


Jack said...

Thank you thousands of times !!! Your precious piece of advice has been of unprecedented help for me, especially for managing the simultaneous filling of forms (with DropDownLists) by different users.
I really thank you.
Jack.


Anonymous said...

I agree, it's I like, probably in the future ...


Anonymous said...

No bad :), it's unusual, to I come in handy


Anonymous said...

Hey - I am definitely happy to find this. great job!


Anonymous said...

Hi,
Is it possible to use both Viewstate variable instead of session variable?
Please advise
Thanks


Abuhurera Ansari said...

Nice Post its really help me a lot to solve my problem

Thanks


Unknown said...

this doesn't work if the first thing you do is refresh the page


Anonymous said...

I try the code but Unfortunately didn't work, i build a web page with details view, if i refresh the page the data re-inserted in the database again and again, but if i click the button in the code above (Button1) it gave me error in the source :
this is the message :
NullReferenceException

what to do ???

thanks a lot


Find More Articles