Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HtmlButton handler fires twice when clicked (when AutoEventWireup="True")

Tags:

asp.net

I have an html button, see below. When it's clicked and AutoEventWireup="true", the Save_Click click handler is fired twice. When AutoEventWireup="False", it fires once.

Why is it firing twice? The button is not registered twice and no code which is adding the event handler. Using master page and no Ajax.

<button id="Save" accesskey="v" type="submit" runat="server" onserverclick="Save_Click"></button>
like image 705
Tony_Henrich Avatar asked Oct 13 '09 19:10

Tony_Henrich


Video Answer


2 Answers

And now (at least in .net 4) even better:

<button runat="server">

by default behaves as it has type="submit" (fires twice on click), so for it to work correctly, we should explicitly set type="button", i.e.:

<button id="ButtonSubscribe2" runat="server" type="button" onserverclick="Save_Click"></button>
like image 150
Andriy F. Avatar answered Sep 25 '22 11:09

Andriy F.


Ok I found out that that an HTMLButton fires for the onserverclick event and for the type="submit". When I removed type="submit", it fires once. This quirky behavior took me a long time to discover!

like image 32
Tony_Henrich Avatar answered Sep 23 '22 11:09

Tony_Henrich