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
01
protected
void
Page_Load(
object
sender, EventArgs e)
02
{
03
if
(!IsPostBack)
04
{
05
Session[
"CheckRefresh"
] =
06
Server.UrlDecode(System.DateTime.Now.ToString());
07
}
08
}
09
protected
void
Button1_Click(
object
sender, EventArgs e)
10
{
11
if
(Session[
"CheckRefresh"
].ToString() == ViewState[
"CheckRefresh"
].ToString())
12
{
13
Label1.Text =
"Hello"
;
14
Session[
"CheckRefresh"
] =
15
Server.UrlDecode(System.DateTime.Now.ToString());
16
}
17
else
18
{
19
Label1.Text =
"Page Refreshed"
;
20
}
21
}
22
23
protected
void
Page_PreRender(
object
sender, EventArgs e)
24
{
25
ViewState[
"CheckRefresh"
] = Session[
"CheckRefresh"
];
26
}
VB.NET
01
Protected
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
02
If
Not
IsPostBack
Then
03
Session(
"CheckRefresh"
) = Server.UrlDecode(System.DateTime.Now.ToString())
04
End
If
05
End
Sub
06
Protected
Sub
Button1_Click(sender
As
Object
, e
As
EventArgs)
07
If
Session(
"CheckRefresh"
).ToString() = ViewState(
"CheckRefresh"
).ToString()
Then
08
Label1.Text =
"Hello"
09
Session(
"CheckRefresh"
) = Server.UrlDecode(System.DateTime.Now.ToString())
10
Else
11
Label1.Text =
"Page Refreshed"
12
End
If
13
End
Sub
14
15
Protected
Sub
Page_PreRender(sender
As
Object
, e
As
EventArgs)
16
ViewState(
"CheckRefresh"
) = Session(
"CheckRefresh"
)
17
End
Sub
If you like this post than join us or share
12 comments:
Thanks .worked Smoothly
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 ?
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
pls explain how this works ..i cant able to understand
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.
I agree, it's I like, probably in the future ...
No bad :), it's unusual, to I come in handy
Hey - I am definitely happy to find this. great job!
Hi,
Is 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
Thanks
this doesn't work if the first thing you do is refresh the page
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
Post a Comment