When user press F5 after a button click event, the click event fires again!

Discussion in 'ASP .Net' started by Max2006, Sep 19, 2007.

  1. Max2006

    Max2006 Guest

    Hi,

    I have a simple form with a button on it. After a button click postback,
    anytime user press F5 (browser page refresh), the button's OnClick event
    handler is called again without the button actually be pressed!

    Is this a bug in ASP.NET? Is there any technique to prevent that?

    I know that a Response.Redirect to the same page solves the issue, however
    because of some requirements; it is not an option for me.

    Any help would be appreciated,
    Max
    Max2006, Sep 19, 2007
    #1
    1. Advertising

  2. Max2006

    Roland Dick Guest

    Re: When user press F5 after a button click event, the click eventfires again!

    Hi Max,

    Max2006 schrieb:
    > I have a simple form with a button on it. After a button click postback,
    > anytime user press F5 (browser page refresh), the button's OnClick event
    > handler is called again without the button actually be pressed!
    >
    > Is this a bug in ASP.NET? Is there any technique to prevent that?


    this is not a bug in ASP.NET, it is by design: The information that the
    button was pressed is actually re-sent to the server (along with all
    other information of the controls) when the page is being refreshed. It
    is not ASP.NET specific (that's why often you find something like "Press
    the pay button only once!" in shopping or payment systems).

    One way around this that comes to my mind right now could be to assign
    the session a GUID and store the GUID in the OnClick handler of the
    server side code in an application-wide hashtable or arraylist. Then you
    can check whether the user pressed the button several times. For example:

    public void OnClick()
    {
    if
    (((Hashtable)Application["UserOKHashtable"]).ContainsKey(Session["UserSessionKey"]))
    {
    // double - clicked or user pressed refresh
    return;
    }
    else
    {

    ((Hashtable)Application["UserOKHashtable"]).Add(Session["UserSessionKey"],
    UserName);
    // your code here
    // and finally

    ((Hashtable)Application["UserOKHashtable"]).Remove(Session["UserSessionKey"]);
    }
    }

    I haven't tried this code yet, it's just for illustration purposes if
    noone comes up with a better idea :)
    Remember to remove the user's session key from the hashtable at the end
    of the procedure, otherwise the user will only be able to click the
    button once during the whole session; if that is the behavoiur you want,
    call the remove in the Session_End event in global.asax instead.

    Hope this helps,

    Roland
    Roland Dick, Sep 19, 2007
    #2
    1. Advertising

  3. Hi Max,

    This issue is very common in web programming. If you search for "prevent
    double post", I'm sure you will find more information and possible
    workaround for it.


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Sep 19, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    820
    Purvi T
    Oct 19, 2004
  2. Jie
    Replies:
    7
    Views:
    6,124
    Xavier Pacheco
    Oct 9, 2004
  3. ad
    Replies:
    4
    Views:
    828
    jasonkester
    Aug 26, 2005
  4. Asaf
    Replies:
    1
    Views:
    683
  5. Replies:
    3
    Views:
    981
Loading...

Share This Page