Monday, December 7, 2009

How to detect page refresh using asp.net

Most of the forum i found this problem. Thats 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 is the solution. We will use this technique to resolve this issue.


An Alternative Solution:
Let you have an entry page named frmArticle. 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 breowser refresh button the frmArticle page will just load again with empty textbox controls.


Now add a page into your project. And write the below MARKUP code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Detect_Page_Refresh.aspx.cs" Inherits="Detect_Page_Refresh" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>How to Detect Page Refresh</title>
</head>

<body>
<form id="form1" runat="server">
<div>

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

</div>
</form>
</body>

</html>

Now in code behind write the below code:
using System;

public partial class Detect_Page_Refresh : System.Web.UI.Page
{

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

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

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....");
}
}

Output:

Fig: Output

The main logic is we wiil use two variables. One is Session variable & another one ViewStatevariable. Initialy bothe variable contain same time so that when user click on button thenthe condition will be satisfied & execute code that we want. 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 satisfyand write "Page Refresh Detected....". If you cant understand yet then please read the belowexpalanation. Otherwise skip.

Code Explanation:
When you run the project then at first Page_Load evebt fired.Means now Session variable Check_Page_Refresh contains the current date time valuelike "12/7/2009 11:20:14 AM". After that Page_PreRender method will be executed means Viewstate variable Check_Page_Refresh assigned by Session state variable Check_Page_Refresh value. So at that moment both ViewState & Session variable is same.

Now page is open and user click on button. So the Page_Load event wiil be fired but Session variable will not set since the action is PostBack action. After that btn_Click event fired & 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. Let now the Session state variable holds value "12/7/2009 11:22:14 AM". Now the Page_PreRender event also executed after btn_Click event so that both Session variable and ViewState variable will be same means "12/7/2009 11:22:14 AM". So if user again click on the button same scenario wll be executed since both Session and ViewState variable is same & also both variable reset to the current time.

Ok now if user click on Refresh 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 variablere 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/2009 11:20:14 AM" instead of "12/7/2009 11:22:14 AM" where as Session state variable is "12/7/2009 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 cases.

2 comments:

Anonymous said...

Thanks!

Test ASP said...

Nice....

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash