Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

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.

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)

Share

About the Author

Namita Patil

India India
No Biography provided

Comments and Discussions

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