Wednesday, April 29, 2009

ASP.NET event firing twice

First Solution:
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:
  • 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.
e.g: 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.

Second Solution:

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".

No comments:

Post a Comment

Heaven - it is ok but try to earn money at hellishdollars.com !