locked
what is the value of Page.IsPostBack when user Pressed F5 ? RRS feed

  • Question

  • User-1096667618 posted

    I thought when user pressed Ctrl + F5 will cause page restart, then IsPostBack will be false.

    But I found when user only pressed F5 will also leed the page restart, so IsPostBack is also false...


    So, if users press F5 unstopped,  will it be a problem like too much pressure for server?

    is there any way that user pressed F5 we can set the IsPostBack value to be true?


    Thanks!

    Sunday, August 2, 2009 10:52 AM

Answers

  • User-158764254 posted

    pressing F5 in the browser, causes it to repeat the last request that it made. 

    So if your user visits a page for the first time (not a postback) and then presses F5 - that F5 generated request will also not be a PostBack.

    But, if your user visits a page (not a postback) and then clicks a button - causing a postback and raising a click event, and then presses F5 - that F5 generated request will also be a PostBack and will also raise that same click event.

    You cant alter whether IsPostBack is true or not, but there are techniques for detecting that the postback was caused by an F5 refresh.

    for example:

        http://forums.asp.net/t/763984.aspx

        http://msdn.microsoft.com/en-us/library/ms379557(VS.80).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 2, 2009 11:30 AM

All replies

  • User-158764254 posted

    pressing F5 in the browser, causes it to repeat the last request that it made. 

    So if your user visits a page for the first time (not a postback) and then presses F5 - that F5 generated request will also not be a PostBack.

    But, if your user visits a page (not a postback) and then clicks a button - causing a postback and raising a click event, and then presses F5 - that F5 generated request will also be a PostBack and will also raise that same click event.

    You cant alter whether IsPostBack is true or not, but there are techniques for detecting that the postback was caused by an F5 refresh.

    for example:

        http://forums.asp.net/t/763984.aspx

        http://msdn.microsoft.com/en-us/library/ms379557(VS.80).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 2, 2009 11:30 AM
  • User-1096667618 posted

    Thanks man! Very good articles and very easy solution!

    Monday, August 3, 2009 12:16 AM
  • User-1096667618 posted

    Thanks man! Very good articles and very easy solution!

    But if this is a problem, how come asp.net 3.5 still got the same problem? Why it can not solve this like IsPostBack in aspx pages...

    Monday, August 3, 2009 12:18 AM
  • User-151352204 posted

    while pressing f5

     causes last request what user was browsing.

    to stop duplicate Duplicate Record Insertion on Page Refresh refer  following links

    http://aspalliance.com/687_Preventing_Duplicate_Record_Insertion_on_Page_Refresh

    http://www.indiastudychannel.com/resources/18198-CREATING-FIRST-WEB-FORM-IN-ASP-NET-WITH-C.aspx


    i hope it helps you

    Monday, August 3, 2009 2:03 AM
  • User-735357247 posted

    Yes i agree with Mike When user press F5 for the firsttime the IsPostBack will be false.If user clicks some button causing a postback, in this case pressing F5 will make IsPostBack to true

    Monday, August 3, 2009 2:19 AM
  • User-1096667618 posted

    pressing F5 in the browser, causes it to repeat the last request that it made. 

    So if your user visits a page for the first time (not a postback) and then presses F5 - that F5 generated request will also not be a PostBack.

    But, if your user visits a page (not a postback) and then clicks a button - causing a postback and raising a click event, and then presses F5 - that F5 generated request will also be a PostBack and will also raise that same click event.

    You cant alter whether IsPostBack is true or not, but there are techniques for detecting that the postback was caused by an F5 refresh.

    for example:

        http://forums.asp.net/t/763984.aspx

        http://msdn.microsoft.com/en-us/library/ms379557(VS.80).aspx

    Hi! I saw the post, and I did not find your version, just page 2, someone write some codes said he translated the code of yours from VB to C#.

    The code is like this:

     bool _isPostBackRefresh;
            public bool IsPostBackRefresh
            {
                get
                {
                    return (_isPostBackRefresh);
                }
            }
            void DetectPostBackRefresh()
            {
                int lastTicketID = 0;
                int actualTicketID = 1;
                int newTicketID;
                if (HttpContext.Current.Session["TicketID"] != null)
                {
                    lastTicketID = int.Parse(HttpContext.Current.Session["TicketID"].ToString());
                    actualTicketID = int.Parse(HttpContext.Current.Request["__TICKET"]);
                }
                if (actualTicketID > lastTicketID)
                {
                    HttpContext.Current.Session["TicketID"] = actualTicketID;
                    _isPostBackRefresh = false;
                }
                else
                    _isPostBackRefresh = true;
                newTicketID = actualTicketID + 1;
                ClientScript.RegisterHiddenField("__TICKET", newTicketID.ToString());
            }
            protected override void OnLoad(EventArgs e)
            {
                DetectPostBackRefresh();
                base.OnLoad(e);
            }

            bool _isPostBackRefresh;


            public bool IsPostBackRefresh

            {

                get

                {

                    return (_isPostBackRefresh);

                }

            }


            void DetectPostBackRefresh()

            {

                int lastTicketID = 0;

                int actualTicketID = 1;

                int newTicketID;


                if (HttpContext.Current.Session["TicketID"] != null)

                {

                    lastTicketID = int.Parse(HttpContext.Current.Session["TicketID"].ToString());

                    actualTicketID = int.Parse(HttpContext.Current.Request["__TICKET"]);

                }


                if (actualTicketID > lastTicketID)

                {

                    HttpContext.Current.Session["TicketID"] = actualTicketID;

                    _isPostBackRefresh = false;

                }

                else

                    _isPostBackRefresh = true;


                newTicketID = actualTicketID + 1;

                ClientScript.RegisterHiddenField("__TICKET", newTicketID.ToString());

            }


            protected override void OnLoad(EventArgs e)

            {

                DetectPostBackRefresh();

                base.OnLoad(e);

            }


    *********************

    in this phrase:

     if (HttpContext.Current.Session["TicketID"] != null)

                {

                    lastTicketID = int.Parse(HttpContext.Current.Session["TicketID"].ToString());

                    actualTicketID = int.Parse(HttpContext.Current.Request["__TICKET"]);

                }

    there is no such request HttpContext.Current.Request["__TICKET"], so it will get a System.ArgumentNullException, is there any way I can see your original code?

    Thanks!

    Monday, August 3, 2009 11:30 PM