ASP.NET event firing twice

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

This article is part of the GWB Archives. Original Author: Tim Huffam

New on Geeks with Blogs