button click event firing twice

We recently had several button controls that were consistently firing twice on click. After a fair amount of head scratching and feeling stupid, it turns out that we were handling the button click twice - not really surprising that the event was firing twice then.

The reason the click was being handled twice was because the onclick handler was being specified twice: once in the button tag as the OnClick attribute:

<asp:Button ID="myButton" runat="server" Text="my button" OnClick="myButton_Click" />

and once in the code behind

Protected Sub myButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles myButton.Click

Simply removing the OnClick attribute from the button tag solved this problem.


Posted on Friday, February 4, 2011 3:58:00 PM GMT by ylva
Jesh, I did the same, how silly! Thanks for the post, it was what made me look twice ;)
Posted on Monday, April 18, 2011 8:13:00 AM GMT by santosh kakani
Nice article dude

i will post same in my blog too
you saved my $12 value
Posted on Friday, July 15, 2011 9:05:00 AM GMT by Chris Crookes
I just did exactly the same thing, I thought it was to do with my method of adding a row to a datagrid and eventually used a response.write() to see what was happening. Stoopid me :(
Posted on Tuesday, September 27, 2011 11:15:00 AM GMT by winston
Awesome, just found the exact same thing in the code I'm working on.
Posted on Wednesday, November 2, 2011 9:36:00 AM GMT by sankar
nice article

Post a comment