Click here to Skip to main content
15,880,891 members
Articles / Web Development / ASP.NET
Article

Refresh Page Issue in ASP.Net

Rate me:
Please Sign up or sign in to vote.
4.74/5 (24 votes)
24 Jul 2008CPOL2 min read 107.4K   971   29   24
If user refreshes page after executing a button click event/method, the same executed event/method gets executed, which it should not do.

Introduction

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.

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

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

License

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Praisereally helpful Pin
yeyskalyn9-Nov-22 7:16
yeyskalyn9-Nov-22 7:16 
QuestionPage refresh issue Pin
Member 1574760225-Aug-22 3:05
Member 1574760225-Aug-22 3:05 
GeneralMuchas gracias! Pin
KrNeS21-Jan-16 7:37
KrNeS21-Jan-16 7:37 
GeneralIssues Details Pin
Member 1113220519-May-15 4:14
Member 1113220519-May-15 4:14 
Generalinvigorate page issue Pin
Member 1110544023-Sep-14 21:47
Member 1110544023-Sep-14 21:47 
QuestionThanks....solution works well! Pin
Ravinder Singh Yadav24-May-14 6:46
Ravinder Singh Yadav24-May-14 6:46 
Thanks for such a simple solution to the problem. It works well.
GeneralMy vote of 5 Pin
Thanks78722-Jul-13 2:15
professionalThanks78722-Jul-13 2:15 
GeneralA little format of my own... just an evolution Pin
WaldemarHaszlakiewicz21-Jan-12 5:53
WaldemarHaszlakiewicz21-Jan-12 5:53 
QuestionError on page Pin
suhasdak22-Nov-11 21:04
suhasdak22-Nov-11 21:04 
AnswerRe: Error on page Pin
pete66778-Aug-12 5:31
pete66778-Aug-12 5:31 
Questionerror Pin
suhasdak22-Nov-11 20:59
suhasdak22-Nov-11 20:59 
GeneralSimple and working solution! Pin
bgmate28-Oct-11 9:53
bgmate28-Oct-11 9:53 
QuestionThanks Pin
Rkoneru28-Sep-11 6:24
Rkoneru28-Sep-11 6:24 
GeneralMy vote of 5 Pin
Rkoneru28-Sep-11 6:23
Rkoneru28-Sep-11 6:23 
QuestionIs it possible to do this without session? Pin
Member 343803811-Apr-11 19:37
Member 343803811-Apr-11 19:37 
Generalerror: Pin
eagle passioned19-Oct-10 2:42
eagle passioned19-Oct-10 2:42 
GeneralMy vote of 5 Pin
sampada Shridhar Gaygol6-Aug-10 0:33
sampada Shridhar Gaygol6-Aug-10 0:33 
Generalrefresh page issue Pin
turboss14-Jul-10 3:03
turboss14-Jul-10 3:03 
Generalits working for some background Pin
ush@286-Apr-10 20:13
ush@286-Apr-10 20:13 
GeneralIssue Pin
Sajid Wahab18-Feb-09 21:09
Sajid Wahab18-Feb-09 21:09 
GeneralRe: Issue Pin
Maheswari Priya12-May-09 4:18
Maheswari Priya12-May-09 4:18 
GeneralRe: Issue Pin
turboss14-Jul-10 3:05
turboss14-Jul-10 3:05 
GeneralGood solution Pin
naughtygirl_sony4-Feb-09 0:59
naughtygirl_sony4-Feb-09 0:59 
GeneralRemove/fix the source code formatting Pin
leppie25-Jul-08 5:46
leppie25-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.