Geeks With Blogs

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

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 C# .NET , ASP.NET , ASP.NET AJAX | Back to top


Comments on this post: ASP.NET event firing twice

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

# re: ASP.NET event firing twice
Requesting Gravatar...
Clear and simple explanation, but to the point. I wish this was the first posting I saw after searching online for a while.

Thanks.
Left by Nan on Mar 29, 2007 8:34 PM

# re: ASP.NET event firing twice
Requesting Gravatar...
it was quite helpful my problem get solved.
thank you.
Left by tarun on Jun 12, 2007 2:47 AM

# re: ASP.NET event firing twice
Requesting Gravatar...
is it the IDE thats hooking the event 2x due to moving up to vs2005?
Left by m on Jul 18, 2007 2:15 PM

# re: ASP.NET event firing twice
Requesting Gravatar...
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
Left by Korayem on Feb 18, 2008 8:17 AM

# re: ASP.NET event firing twice
Requesting Gravatar...
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.
Left by Ernest on Apr 05, 2008 8:41 AM

# re: ASP.NET event firing twice
Requesting Gravatar...
Very useful, too the point explanation, thanks
Left by B Sandhu on May 11, 2008 7:17 PM

# re: ASP.NET event firing twice
Requesting Gravatar...
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".
Left by ap on Jul 06, 2008 8:05 AM

# re: ASP.NET event firing twice
Requesting Gravatar...
Works great! Thank you for the solution!
Left by Laurie on Sep 05, 2008 2:36 AM

# re: ASP.NET event firing twice
Requesting Gravatar...
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.
Left by Saikumar Jalda on Sep 11, 2008 7:04 PM

# re: ASP.NET event firing twice
Requesting Gravatar...
Solved my problem! Thanks
Left by Hanlet on Feb 20, 2009 10:20 AM

# re: ASP.NET event firing twice
Requesting Gravatar...
Thanks man... It works great.... it was the solution to my problem after a lot of hours searching!!!!
Thanks again
Left by kostas on Sep 03, 2009 5:01 AM

# re: ASP.NET event firing twice
Requesting Gravatar...
Mine was a little different, but this pointed me in the right direction - thanks!!

For those using VB.NET:
I had the OnClick="btnTest_Click" on the asp page and Handles btnTest.OnClick in the code page. Removing one of them did the trick.
Left by bd on Sep 18, 2009 1:36 PM

# re: ASP.NET event firing twice
Requesting Gravatar...
solved my problem my program is working

Thanks
Left by kuldeep on Jan 11, 2010 1:48 AM

# re: ASP.NET event firing twice
Requesting Gravatar...
thanks fren..ur workaround solve my problem
Left by renu on Jan 22, 2010 4:11 AM

# re: ASP.NET event firing twice
Requesting Gravatar...
Chaning it from "submit" to "button" type worked! Thank you so much!
Left by Tim Chapman on Mar 02, 2010 3:32 PM

# re: ASP.NET event firing twice
Requesting Gravatar...
it didn't work for me.i want 2 click events on one button to call different function
Left by sanika on May 21, 2010 6:53 AM

# re: ASP.NET event firing twice
Requesting Gravatar...
Thanks a lot
Left by Nishant on Apr 18, 2011 8:26 AM

# re: ASP.NET event firing twice
Requesting Gravatar...
Great solution work fine
Left by Vijay Kumar on May 02, 2011 8:41 AM

# re: ASP.NET event firing twice
Requesting Gravatar...
Great! It works
Left by fewfewf on May 05, 2011 12:26 PM

# re: ASP.NET event firing twice
Requesting Gravatar...
Just wanted to say thank you. I have been struggling with this issue on and off for about 3 days now and was amazed at how simple the fix was.
Left by mitch on Jul 06, 2011 2:13 PM

# re: ASP.NET event firing twice
Requesting Gravatar...
Thanks a lot. I've been sat in the office wasting time testing all my methods until I came across this. Thank you so much
Left by Lukasz on Aug 03, 2011 8:39 AM

# re: ASP.NET event firing twice
Requesting Gravatar...
Thanks a lot... very good & quick solotion:)
Left by Deepak on Sep 29, 2011 2:19 AM

# re: ASP.NET event firing twice
Requesting Gravatar...
Thnx very much! quite simple description...

...
Left by Ronit on Nov 13, 2011 11:25 PM

# re: ASP.NET event firing twice
Requesting Gravatar...
Thanks, It helps me ....
Left by Rajendra on Nov 20, 2011 10:55 PM

# re: ASP.NET event firing twice
Requesting Gravatar...
Your solution was exactly what I needed. It was hard to find your article (not your fault) because searches kept coming up with articles on how to prevent a user from manually clicking on the submit button twice.
Left by Forrest C. Shields II on Jul 31, 2012 5:36 PM

# re: ASP.NET event firing twice
Requesting Gravatar...
It worked. Thanks a lot.
Left by sapna on Dec 14, 2012 11:37 PM

# re: ASP.NET event firing twice
Requesting Gravatar...
Thanks....It Helped me........
Left by Dil on Jan 14, 2013 10:27 PM

# re: ASP.NET event firing twice
Requesting Gravatar...
Thank you so much....Very good solution....
Left by Ranjani Sekar on Mar 24, 2013 11:00 PM

Your comment:
 (will show your gravatar)


Copyright © Tim Huffam | Powered by: GeeksWithBlogs.net