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
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
VB.NET
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
Thanks .worked Smoothly
ReplyDeleteyhanks for the nice code work perfectly,
ReplyDeletei am just wondering is possible to change handler from refreshe to back button browser , please tell me how ?
The code is working fine. Can you explain how it works?
ReplyDeleteBecause 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
pls explain how this works ..i cant able to understand
ReplyDeleteThank 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.
ReplyDeleteI really thank you.
Jack.
I agree, it's I like, probably in the future ...
ReplyDeleteNo bad :), it's unusual, to I come in handy
ReplyDeleteHey - I am definitely happy to find this. great job!
ReplyDeleteHi,
ReplyDeleteIs it possible to use both Viewstate variable instead of session variable?
Please advise
Thanks
Nice Post its really help me a lot to solve my problem
ReplyDeleteThanks
this doesn't work if the first thing you do is refresh the page
ReplyDeleteI 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 :
ReplyDeletethis is the message :
NullReferenceException
what to do ???
thanks a lot