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