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

M

Max2006

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
 
R

Roland Dick

Hi Max,
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
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top