473,396 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Sep 19 '07 #1
2 4947
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
Sep 19 '07 #2
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 (wa****@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.

Sep 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: sanjay patil via .NET 247 | last post by:
Hello, I have a problem with web image button control click event. I am saving a record to database on click event of this image button. When click event fires first time it executes code...
6
by: Mark | last post by:
I have two dropdown lists. Both have autopostback set to true. In both dropdowns, when you select an item from the list, it redirects to the Value property of the dropdown. Nothing fancy. ...
8
by: walesboy | last post by:
greetings - I have a btnSubmit button with a Handles btnSubmit.click which works great if all the user does is click that button. But, if the user ALSO changes a text box on the page (which...
6
by: guoqi zheng | last post by:
In a regular html form, when user press "enter" key, the form will be submitted. However, in ASP.NET web form, a form will only be submitted (post back) when a special button is clicked. Many...
11
by: Joe | last post by:
Hello All, I have an ASP.NET page with one Textbox (SearchTextBox) and one ImageButton (SearchButton) server controls. The user can type search text in SearchTextBox and click SearchButton and...
30
by: Charles Law | last post by:
Here's one that should probably have the sub-heading "I'm sure I asked this once before, but ...". Two users are both looking at the same data, from a database. One user changes the data and...
3
by: Patrick [MSFT] | last post by:
Let me preface this with the goal I'm trying to achieve is mimic a feature of another language (Dexterity used by Microsoft Dynamics) and so while a filling a drop down list is a workable solution...
4
by: nkoier | last post by:
Hi, I've been going crazy trying to figure out what's wrong with our Asp.Net 2.0 intranet site. At the very top of our main page I provide a TextBox and a Button for submitting Google searches....
1
by: daonho | last post by:
I tried to use javascript to trigger up the button click function when user press enter key from the textbox. This function work fine with a single button click such has login page. However, if the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.