Nannette Thacker Web Application Development



See all Blog Posts by Nannette.

Nannette Thacker, consultant and owner of Shining Star Services LLC, specializes in development of custom dynamic database driven web applications utilizing technologies. Nannette has been developing ASP sites since 1997. Nannette has written numerous articles on web development techniques and tutorials.

Nannette is the owner and developer of

 Subscribe in a reader

View Nannette  Thacker's profile on LinkedIn

OnClick and Handles Caution: Events Firing Twice

By Nannette Thacker

If you're new to ASP.NET, let me offer you a word of caution in regard to using the onclick event within your controls and the Handles key word within your procedure declarations. To demonstrate this, let's create a web form and add these two controls:

<asp:Button ID="Button1" runat="server" Text="Button"/><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

Double-click the button in your Design view, and you will be taken to the codebehind where you may see that a new Button1_Click Sub has been created for you. Notice the Handles Button1.Click key word has automatically been added to the end of the procedure declaration to handle the Button Click event.

Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

End Sub

If you utilize the code as generated, you are safe. However, what if you are copying and pasting from someone else's snippets? You may see onclick="Button1_Click" added to the button:

<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"/><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

Beware of this common mistake. You must choose either the onclick within the button or the Handles keyword within the procedure declaration. Let's look at what happens if you have both in your code. To test this, I altered my Label1 to add the value of Label1 and the string " test 1 : " and display it to the screen.

Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = Label1.Text & " test 1 : "
End Sub

When testing in Internet Explorer 7.0, each time I hit the page, IE would close the browser and ask to send an error report. When I ran the code in FireFox, my label displayed this result:

Label test 1 : test 1 : 

In other words, the code is hit twice, which may not only double your processing, but return unexpected results. So simply be aware of this possibility and avoid using onclick with Handles.

May your dreams be in!

Nannette Thacker

Posted: Feb 19 2008, 12:41 PM by nannette | with 15 comment(s) |
Filed under:


Joe Chung said:

Another way to add the handler is to use the AddHandler statement in your page's initialization routines:

AddHandler Button1.Click, AddressOf Button1_Click

That's what adding event handlers declaratively with server control attributes like OnClick or with the Handles keyword do under the hood.

# February 19, 2008 8:21 PM

rrobbins said:

This mistake caused me some problems today. I had two emails going out because the submit button's click event was firing twice. This was in a project that I migrated to ASP.NET 2.0 from ASP.NET 1.1.

# February 28, 2008 7:05 PM

rjdudley said:

Well that was easy!  Thanks for the explanation, this was an issue in code I inherited.

# September 18, 2008 3:33 PM

Chris said:

Thanks Nanette.  I was just experiencing this issue.

# January 21, 2009 6:48 PM

Ivan said:

Thanks Nanette... I was scratching my head on this one!

# March 17, 2009 11:25 AM

Steve said:

Fantastic! Had me for 3 hours!! First click on Google got me here with the answer. Thanks Nanette.

# July 10, 2009 6:47 AM

Tom said:

Just wanted to say thanks, this one had me going for a little while, was doing an example from site and ran into this. NEVER TAKE THIS PAGE DOWN... looks like its been helping people for over a year.

# July 31, 2009 2:34 PM

Evan said:

I too must say thanks.

I was writeing an HTA in vbscript for our support group. The "Find" button found every other entry. I could not figure out how the Find_OnClick sub was getting called twice.

# August 10, 2009 5:16 PM

RK said:

I do not have Handles Button1.Click in codebehind, just used onclick in aspx page, still it fires twice. Any help would be appreciated.

# October 20, 2009 5:12 PM

hema said:

Thank you, you saved my time

# October 21, 2009 5:55 AM

Chandu said:

Thank you, very much

# December 8, 2009 10:53 PM

Lin said:

Thank you so very much.

# February 11, 2010 4:56 AM

Sangeetha said:

Really helped me deleted the control and again pasted the code...:-)

thank you.

# July 1, 2010 5:26 AM

Prakash babu said:

Thank you very much. Your solution has solved the problem

# September 20, 2010 1:57 AM

John said:

I've got an issue where it fires twice, but only sometimes.  :p  AutoEvenWireup is false, it's not defining an OnClick in the code.  It's driving me mad because I can't recreate it with consistency, but about every 3rd or 4th click it executes twice.  Urg.

# December 10, 2010 12:52 PM

nannette said:


that would be maddening!

Can you put a breakpoint on the code that is firing twice, then step out of the onclick code both times with an F11 and see what the calling program is that executed the method.

With it being inconsistent you'll have to frustratingly step through a lot of successful attempts.


# December 12, 2010 4:45 PM

B.K.Ganga Bhavani said:

Design is Good Keep It Up

# December 14, 2010 3:12 AM

monica said:

John, did you figure out your issue?  I only define onclick in aspx and have no event handler in the code behind.  Only sporadically onclick gets fired twice...tried with both AutoEventWireup true and false and same random behavior.

# December 14, 2010 6:55 PM

Edwin said:

Just like to share a bit regarding on this topic. I had experiencing this problem before. I'm using javascript on client side operation. This my example:

I let the code on aspx for onclick="something_click" and add some onclientclick="return somejavascript();"

on code behind i remove handles

but add with

something.attributes.add("onclick","Javascript:return Somejavascript")

that my ten cent.


# April 4, 2011 11:05 PM

Jamin said:

thank you Nanette for this tip from Geneva.

# February 11, 2012 7:13 PM
Leave a Comment