Tim Huffam

Dotting the I and crossing the T of I.T.

  Home  |   Contact  |   Syndication    |   Login
  129 Posts | 0 Stories | 874 Comments | 677 Trackbacks

News

Archives

Post Categories

Interesting Blogs/Links

This seems to be a very common problem where by your event handler method gets run twice.

It is caused by VS.NET inserting 2 wireup of the event handler:

- once in the aspx (HTML) eg:  <asp:Button ... OnClick="btnTest_Click" />

- and once in the VS.NET generated section (InitializeComponent) eg:
  this.btnTest.Click += new System.EventHandler(this.btnTest_Click);

The easiest solution, imo, is to simply remove the "OnClick..." HTML markup from the .aspx page.

Eg change this:
  <asp:Button ID="btnTest" runat="server" Text="Test" OnClick="btnTest_Click" />

To this:
  <asp:Button ID="btnTest" runat="server" Text="Test"  />

Rebuild, and bingo! - 1 event per click.

HTH

Tim

posted on Monday, October 23, 2006 4:27 PM

Feedback

# re: ASP.NET event firing twice 10/24/2006 10:17 AM Jannik Anker
Also, you may look into the AutoEventWireup property in the @Page directive...

# re: ASP.NET event firing twice 3/29/2007 8:34 PM Nan
Clear and simple explanation, but to the point. I wish this was the first posting I saw after searching online for a while.

Thanks.

# re: ASP.NET event firing twice 5/20/2007 6:31 AM Mike
It didn't work

# re: ASP.NET event firing twice 6/12/2007 2:47 AM tarun
it was quite helpful my problem get solved.
thank you.

# re: ASP.NET event firing twice 7/18/2007 2:15 PM m
is it the IDE thats hooking the event 2x due to moving up to vs2005?

# re: ASP.NET event firing twice 2/18/2008 8:17 AM Korayem
Ahaa...that explains it.

I went to my masterpage and found that AutoEventWireup="true". And for ALL pages, AutoEventWireup is set also to "true".

By ensuring only one AutoEventWireup="true" exists, the events were fired only once!

Thanks

# re: ASP.NET event firing twice 4/5/2008 8:41 AM Ernest
If you have OnClick="btnTest_Click" in the aspx code, you don't need the Handles btnTest.Click when you declare btnTest_Click in the aspx.vb code.

# re: ASP.NET event firing twice 5/11/2008 7:17 PM B Sandhu
Very useful, too the point explanation, thanks

# re: ASP.NET event firing twice 7/6/2008 8:05 AM ap
I had the same problem, but none of the solutions above solved my problem..

I was using HTML controls on my aspx page like <button runat="server" id="btnNext" type="submit" onserverclick="btnNext_Click" />

The solution for me was to change the type="submit" to type="button".

# re: ASP.NET event firing twice 9/5/2008 2:36 AM Laurie
Works great! Thank you for the solution!

# re: ASP.NET event firing twice 9/11/2008 7:04 PM Saikumar Jalda
While using repeater control.
I place a delete button in gridview but for very page load the itemcommand is firing and the record get deleted.
How to overcome this problem.

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 8 and 2 and type the answer here: