Click here to Skip to main content
13,141,157 members (62,721 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 24 Jul 2008

Refresh Page Issue in ASP.Net

, 24 Jul 2008
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

You may also be interested in...

Comments and Discussions

GeneralMuchas gracias! Pin
KrNeS21-Jan-16 7:37
memberKrNeS21-Jan-16 7:37 
GeneralIssues Details Pin
Member 1113220519-May-15 4:14
memberMember 1113220519-May-15 4:14 
Generalinvigorate page issue Pin
Member 1110544023-Sep-14 21:47
memberMember 1110544023-Sep-14 21:47 
QuestionThanks....solution works well! Pin
Ravinder Singh, PMP24-May-14 6:46
memberRavinder Singh, PMP24-May-14 6:46 
GeneralMy vote of 5 Pin
Rohan Leuva2-Jul-13 2:15
professionalRohan Leuva2-Jul-13 2:15 
GeneralA little format of my own... just an evolution Pin
WaldemarHaszlakiewicz21-Jan-12 5:53
memberWaldemarHaszlakiewicz21-Jan-12 5:53 
QuestionError on page Pin
suhasdak22-Nov-11 21:04
membersuhasdak22-Nov-11 21:04 
AnswerRe: Error on page Pin
cmoorehead458-Aug-12 5:31
membercmoorehead458-Aug-12 5:31 
Questionerror Pin
suhasdak22-Nov-11 20:59
membersuhasdak22-Nov-11 20:59 
GeneralSimple and working solution! Pin
bgmate28-Oct-11 9:53
memberbgmate28-Oct-11 9:53 
QuestionThanks Pin
Rkoneru28-Sep-11 6:24
memberRkoneru28-Sep-11 6:24 
GeneralMy vote of 5 Pin
Rkoneru28-Sep-11 6:23
memberRkoneru28-Sep-11 6:23 
QuestionIs it possible to do this without session? Pin
Member 343803811-Apr-11 19:37
memberMember 343803811-Apr-11 19:37 
Generalerror: Pin
eagle passioned19-Oct-10 2:42
membereagle passioned19-Oct-10 2:42 
GeneralMy vote of 5 Pin
sampada Shridhar Gaygol6-Aug-10 0:33
membersampada Shridhar Gaygol6-Aug-10 0:33 
Generalrefresh page issue Pin
turboss14-Jul-10 3:03
memberturboss14-Jul-10 3:03 
Generalits working for some background Pin
ush@286-Apr-10 20:13
memberush@286-Apr-10 20:13 
GeneralIssue Pin
Sajid Wahab Khan18-Feb-09 21:09
memberSajid Wahab Khan18-Feb-09 21:09 
GeneralRe: Issue Pin
Member 316627612-May-09 4:18
memberMember 316627612-May-09 4:18 
GeneralRe: Issue Pin
turboss14-Jul-10 3:05
memberturboss14-Jul-10 3:05 
GeneralGood solution Pin
naughtygirl_sony4-Feb-09 0:59
membernaughtygirl_sony4-Feb-09 0:59 
GeneralRemove/fix the source code formatting Pin
leppie25-Jul-08 5:46
memberleppie25-Jul-08 5:46 

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

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

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