Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
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

This is a final destinaion

http://www.codeproject.com/KB/aspnet/RefreshPage.aspx

OR

C#
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 ===================
    }
}


C#
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..........!
 
Share this answer
 
v2
Comments
Niranjan dotnet 11-Jun-14 8:35am    
Thank u very much sir....................
Member 12275466 17-Nov-16 6:04am    
it worked for me thank u
MohmdNader 21-Oct-17 4:12am    
Thanks .. It works efficiently
Add the following code in your Page_Load event, and it will be all fine.

C#
//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
 
Share this answer
 
v2
Comments
silverDrops 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?
[no name] 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/ 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/ 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 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.
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[^]
 
Share this answer
 
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
 
Share this answer
 
Comments
Ankur\m/ 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.
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.
 
Share this answer
 
v2
Comments
Ankur\m/ 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.
[no name] 1-Apr-11 22:23pm    
You deleted the code.
Ankur\m/ 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/ 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900