How to Detect Page Refresh in Asp.net C# VB.Net

Most of the forum I found this problem. That’s why in this article I will try to explain how one can detect Browser Refresh button has been clicked by user. Why detection is necessary because let you have an entry page, when user click to save button then the data saved into the database. The problem is now if user click on Browser Refresh button then the same set of data again inserted into the database because Refresh button always perform user last action. Which is the problem as well as this article will provide the resolution. We will use this technique to resolve this issue.

An Alternative Solution:
Let you have an entry page. Then you can block multiple time insert operation regarding page refresh by just redirect to the same page just after your insertion. Since the refresh works on last event so that if user click on Browser Refresh button the page will just load again with empty TextBox controls.

Objective/Outcome:
How to detect page refresh using asp.net CSharp VB.Net

To Detect Page Refresh Add a Button Like Below:

<asp:Button runat="server" ID="btn" Text="Click Me" onclick="btn_Click" />

Now add a Session Variable Under Page Load:
C# Code:

        if (!IsPostBack)
            Session["Check_Page_Refresh"] = DateTime.Now.ToString();

VB.Net Code:

        If Not IsPostBack Then
            Session("Check_Page_Refresh") = DateTime.Now.ToString()
        End If

Now need to add a ViewState Variable under Page PreRender Method:
C# Code:

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

VB.Net Code:

    Protected Overrides Sub onPreRender(ByVal e As System.EventArgs)
        ViewState("Check_Page_Refresh") = Session("Check_Page_Refresh")
    End Sub

Now implement the Button Click Event:
C# Code:

    protected void btn_Click(object sender, EventArgs e)
    {
        if (ViewState["Check_Page_Refresh"].ToString() == Session["Check_Page_Refresh"].ToString())
        {
            Response.Write("Detect Postback or User Event fired....");
            Session["Check_Page_Refresh"] = DateTime.Now.ToString();
        }
        else
            Response.Write("Page Refresh Detected....");
    }

VB.Net Code:

    Protected Sub btn_Click(sender As Object, e As System.EventArgs) Handles btn.Click
        If ViewState("Check_Page_Refresh").ToString() = Session("Check_Page_Refresh").ToString() Then
            Response.Write("Detect Postback or User Event fired....")
            Session("Check_Page_Refresh") = DateTime.Now.ToString()
        Else
            Response.Write("Page Refresh Detected....")
        End If
    End Sub

Note: The main logic is we will use two variables. One is Session variable and another one is ViewState variable. Initially both variable contain same time so that when user click on button then the condition will be satisfied and execute code that required. When user click on Browser Refresh button then the ViewState variable reset to the time of page_load event. So btn_click event condition will not satisfy and write “Page Refresh Detected….”. If you cant understand yet then please read the below explanation. Otherwise skip.

Code Explanation:
When you run the project then at first Page_Load event fired. Means now Session variable Check_Page_Refresh contains the current date time value like “12/7/2014 11:20:14 AM”. After that Page_PreRender method will be executed as per page life cycle means Viewstate variable Check_Page_Refresh assigned by Session state variable Check_Page_Refresh value. So at that moment both ViewState and Session variable is same.

Now page is open and user click on button. So the Page_Load event will fire but Session variable will not set since the action is PostBack action. After that btn_Click event fired and will display “Detect Postback or User Event fired….” since both Sessionstate variable and ViewState variable are same. And also do not forget that here the Session state variable also reset. Now let the Session state variable holds value “12/7/2014 11:22:14 AM”. And the Page_PreRender event also executed after btn_Click event so that both Session variable and ViewState variable will be same, means “12/7/2014 11:22:14 AM”. So if user again click on the button same scenario will be executed since both Session and ViewState variable is same and also both variable reset to the current time.

Ok now if user click on Browser Refresh button then what happend? This is the main portion that you have to understand to detect Page Refresh. Steps were: First Page_Load event fired but Session variable not set since the action is PostBack. After that btn_Click event fired. We knew that before click on Refresh button both variable were same but when user click on Refresh button then the ViewState variable is set to previous one before button click event. Means refresh button will perform from user last event. So now the ViewState variable is reset to “12/7/2014 11:20:14 AM” instead of “12/7/2014 11:22:14 AM” where as Session state variable is “12/7/2014 11:22:14 AM”. So now you can detect easily that the user click on Refresh button. It will be better to debug the code in each event. So that you can easily understand the scenarios.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

     

*