Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Max2006 - 19 Sep 2007 02:25 GMT
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
Roland Dick - 19 Sep 2007 05:46 GMT
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
Walter Wang [MSFT] - 19 Sep 2007 06:37 GMT
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 (wawang@online.microsoft.com, 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.