Click here to Skip to main content
Click here to Skip to main content

Refresh Page Issue in ASP.Net

, 24 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
If user refreshes page after executing a button click event/method, the same executed event/method gets executed, which it should not do.


In Web Programming, the refresh click or postback is the big problem which generally developers face. So in order to avoid the refresh page issues like, if user refreshes the page after executing any button click event/method, the same method gets executed again on refresh. But this should not happen, so here is the code to avoid such issues.

Using the Code

Here the page's PreRender event and ViewState variable helps to differentate between the Button click event call or the Refresh page event call. For example, We have a web page having button to display text entered in text box to the Label.

So when page is first time loaded, then a session["update"] object is assigned by some unique value, and then the PreRender event is being called, where that session is assigned to the viewstate variable.

And on button click event, the session assigning code from page load will never called, as it is postback, so it directly calls the button click event where there is check whether the session and viewstate variables have same value. Here both values will be same. At the end of the click event method, the session variable is assigned with new unique value. Then always after click event, the PreRender event gets called where that newly assigned session value is assigned to viewstate variable.

So whenever the page is being refreshed, viewstate value will become previous value (previous value is taken from viewstate hidden control) which will never match with current session value. So whenever the control goes in button click event, the match condition never gets satisfied hence code related to button click never gets executed.

Points of Interest

Here we can understand the page events flow as well as Viewstate variable's workflow easily.

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"];


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


About the Author

Namita Patil

India India
No Biography provided

Comments and Discussions

Generalinvigorate page issue PinmemberMember 1110544023-Sep-14 22:47 
QuestionThanks....solution works well! PinmemberRavinder Singh, PMP24-May-14 7:46 
GeneralMy vote of 5 PinprofessionalRohan Leuva2-Jul-13 3:15 
GeneralA little format of my own... just an evolution [modified] PinmemberWaldemarHaszlakiewicz21-Jan-12 6:53 
QuestionError on page Pinmembersuhasdak22-Nov-11 22:04 
AnswerRe: Error on page Pinmembercmoorehead458-Aug-12 6:31 
Questionerror Pinmembersuhasdak22-Nov-11 21:59 
GeneralSimple and working solution! Pinmemberbgmate28-Oct-11 10:53 
QuestionThanks PinmemberRkoneru28-Sep-11 7:24 
GeneralMy vote of 5 PinmemberRkoneru28-Sep-11 7:23 
QuestionIs it possible to do this without session? PinmemberMember 343803811-Apr-11 20:37 
Generalerror: Pinmembereagle passioned19-Oct-10 3:42 
GeneralMy vote of 5 Pinmembersampada Shridhar Gaygol6-Aug-10 1:33 
Generalrefresh page issue Pinmemberturboss14-Jul-10 4:03 
Generalits working for some background Pinmemberush@286-Apr-10 21:13 
GeneralIssue PinmemberSajid Wahab Khan18-Feb-09 22:09 
GeneralRe: Issue PinmemberMember 316627612-May-09 5:18 
GeneralRe: Issue Pinmemberturboss14-Jul-10 4:05 
GeneralGood solution Pinmembernaughtygirl_sony4-Feb-09 1:59 
GeneralRemove/fix the source code formatting Pinmemberleppie25-Jul-08 6:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.1 | Last Updated 25 Jul 2008
Article Copyright 2008 by Namita Patil
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid