Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
hi all ,
 
I have a web page in that i have a button and did some coding in that when i refresh page that button click event is firing please let me know how to avoid it on page refresh.
 

Thanking You
 
Mohd Wasif
Posted 1-Apr-11 2:05am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Add the following code in your Page_Load event, and it will be all fine.
 
//browser refresh fires the last event (for ex button click) again 
//the following code prevents this action.
if (!IsPostBack)
{
    ViewState["postids"] = System.Guid.NewGuid().ToString();
    Session["postid"] = ViewState["postids"].ToString();
}
else
{
    if (ViewState["postids"].ToString() != Session["postid"].ToString())
    {
        IsPageRefresh = true;
    }
    Session["postid"] = System.Guid.NewGuid().ToString();
    ViewState["postids"] = Session["postid"].ToString();
}
 
Try and understand the code. Let me know if you don't get it.
 
PS: I took this solution from somewhere on the web (I don't remember the link now) and used it in one of my projects.
 
Cheers!
Ankur
  Permalink  
v2
Comments
silverDrops at 10-Oct-12 11:34am
   
this solution seen logical and simple,but i dosen't work,cause IsPageRefresh always is false,for the reason that this page always not ispostback?
sakthisudhan at 8-Aug-13 7:15am
   
How to avoid the reload page in button click event without viewstate in asp.net ?
pls reply me
Ankur\m/ at 10-Oct-12 12:11pm
   
Did you read the solution carefully or even tried it? The ViewState and Session values are populated when the page loads for the first time. One cannot refresh page unless it is available, right?! Now as the question says, user clicks the button and the page posts back. In this case, the viewstate and session are same and thus isPageRefresh is false; also note that a new guid is generated and stored in viewstate and session. Now the user clicks browser refresh button or F5 button. In this case the session has the updated value but the viewstate will have the older guid value and thus the condition will satisfy and isPageRefresh will become true.
Try putting a debugger around the code and see, if you still don't understand.
Ankur\m/ at 8-Aug-13 8:04am
   
You will then have to store the Guid in a hidden field. Place a hidden field in your page. And replace all ViewState["postids"] with HiddenField1.Value.
 
I just tested it. It works!
silverDrops at 10-Oct-12 13:12pm
   
Thank for your explanation,i have try it before i reply the comment above.I understand the logic,so i try it immediately after i read your solution.Check isRefreshFlag before run my button onclick processing code,and the my processing code never run .So,I reply that comment above,wanna found answer.
Ankur\m/ at 10-Oct-12 13:25pm
   
Sorry I didn't understand the second part of your comment. Do you mean you tried refreshing the page before clicking button click? Yes in that case the page isn't a postback. But the question here asks a method to avoid click event being called again if the page is refreshed after clicking the button. And the solution works very well in that case.
Let me know if I misunderstood your question.
silverDrops at 10-Oct-12 21:58pm
   
my second part means that I have tried putting a debugger around the code and see before I write the first comment,I also wanna find a method to avoid click event being called again if the page is refreshed after clicking the button .I just write a simple test code to test the solution,
Boolean IsPageRefresh;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["postids"] = System.Guid.NewGuid().ToString();
Session["postid"] = ViewState["postids"].ToString();
}
else
{
if (ViewState["postids"].ToString() != Session["postid"].ToString())
{
IsPageRefresh = true;
}
Session["postid"] = System.Guid.NewGuid().ToString();
ViewState["postids"] = Session["postid"].ToString();
}
}
protected void submitBtn_Click(object sender, EventArgs e)
{
if (!IsPageRefresh)
textIn.Value = "not refresh";
else textIn.Value = " refresh";
}
and the textIn.Value always is refresh,if you still understand,I am so sorry my writing is not good,but I still hope you will understand.
Ankur\m/ at 12-Oct-12 4:40am
   
This code is tested and works very well. I have just tested it again for you. You must be doing something wrong. Create a new sample web application project and try again.
silverDrops at 6-Nov-12 3:24am
   
refresh.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="refresh.aspx.cs" Inherits="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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" runat="server" id="textIn" />
<asp:Button ID="submitBtn" runat="server" onclick="submitBtn_Click" />
</div>
</form>
</body>
</html>

refresh.aspx.cs:
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class refresh : System.Web.UI.Page
{
Boolean IsPageRefresh;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["postids"] = System.Guid.NewGuid().ToString();
Session["postid"] = ViewState["postids"].ToString();
}
else
{
if (ViewState["postids"].ToString() != Session["postid"].ToString())
{
IsPageRefresh = true;
}
Session["postid"] = System.Guid.NewGuid().ToString();
ViewState["postids"] = Session["postid"].ToString();
}
}
protected void submitBtn_Click(object sender, EventArgs e)
{
if (IsPageRefresh) textIn.Value = " refresh";
else textIn.Value = "not refresh";
}
}
 
only this simple website for testing,it dosen't work,textIn.value always display refresh.
Ankur\m/ at 6-Nov-12 4:07am
   
I just tested this code. Works perfect!
Steps I followed:
STEP 1: Run the website. IsPageRefresh is false.
STEP 2: Click the button. IsPageRefresh is still false and the text box has 'not refresh' value in it.
STEP 3: Hit F5 or browser refresh, Firefox asks to resend the data, select resend, IsPageRefresh is now true, 'refresh' is shown in the textbox.
 
Follow these steps and let me know what you get. And also let me know what steps you followed so that it always showed refresh.
silverDrops at 27-Nov-12 7:07am
   
I have found the reason fanally!The problem caused by my browser which is Chrome.I thought it is because the way of webkit-inner browser process .net's session is difference from trident-inner browser and gecko-inner browser.So,Your solution works prefect in Firefox and ie,but not in Chrome.
Ankur\m/ at 27-Nov-12 7:32am
   
Oh is it? I never tried it on Chrome. But I would certainly like to check this out. It shouldn't be this way ideally as that's a big difference in the way these engines are handling the viewstate/cookies.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

As metioned in this thread[^] you might consider using a Post/Redirect/Get[^] pattern to avoid this
 
If this isn't applicable to your page, you might want to look at using either Session \ ViewState to handle this
 
e.g. Session
 
http://csharpdotnetfreak.blogspot.com/2008/12/detect-browser-refresh-to-avoid-events.html[^]
 
Refresh Page Issue in ASP.Net[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

This is a final destinaion
 
http://www.codeproject.com/KB/aspnet/RefreshPage.aspx
 
OR
 
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack) // If page loads for first time
    {
        // Assign the Session["update"] with unique value
        Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString()); 
        //=============== Page load code =========================

 

 
        //============== End of Page load code ===================
    }
}
 
protected void btnDisplay_Click(object sender, EventArgs e)
{ 
    // If page not Refreshed
    if (Session["update"].ToString() == ViewState["update"].ToString())
    {
        //=============== On click event code ========================= 

        lblDisplayAddedName.Text = txtName.Text;
 
        //=============== End of On click event code ==================

        // After the event/ method, again update the session 

        Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString()); 
    }
    else // If Page Refreshed
    {
        // Do nothing 
    }
}
 
protected override void OnPreRender(EventArgs e)
{
    ViewState["update"] = Session["update"];
}
 
Great..........!
  Permalink  
v2
Comments
Niranjan dotnet at 11-Jun-14 8:35am
   
Thank u very much sir....................
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Page refresh will fire the same event which has caused the previous postback , so if you refresh the page after clicking the button then button click event will fire
  Permalink  
Comments
Ankur\m/ at 1-Apr-11 7:36am
   
The OP already said that. He wants a solution for the issue i.e how does he prevent the last event to be fired if a user refreshes the page.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Check page load event if there ia any call to that button click event.

Or
try follow to automatic refresh your Page.
Use this between Head tag.
  Permalink  
v2
Comments
Ankur\m/ at 1-Apr-11 7:38am
   
That is a very well known behaviour. Please do not add answer if you don't know a solution. Any clarification goes in the comment section.
Brijesh Kr at 1-Apr-11 22:23pm
   
You deleted the code.
Ankur\m/ at 2-Apr-11 0:28am
   
The was no code there but just PRE tags. You can always check the revision for what has been edited.
Ankur\m/ at 2-Apr-11 0:31am
   
Click on the link - 'Revision 2', Then click 'Show Minor Edits'. Check both the revisions and select Compare. You will see that only 'empty' PRE tags were deleted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 315
1 Jochen Arndt 190
2 DamithSL 125
3 PIEBALDconsult 110
4 Garth J Lancaster 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 22 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100