Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

button firing click event twice

Tags:

asp.net

It seems that sometimes (but not always) my button click event is being fired twice. The fact that it seems to happen sometimes but not always is really puzzling me. Here is my button :

<button id="btnSave" onserverclick="btnAddUser_Click" name="btnSave" type="submit" style="font-size:11px;font-family:Verdana;font-weight:bold;" runat="server">Save</button>
like image 633
user517406 Avatar asked Apr 18 '11 10:04

user517406


2 Answers

I had the same problem, and it took me a while to figure it out! If you add type="button" to your button, it seems to fix the issue.

like image 96
Brandsdal Avatar answered Oct 17 '22 05:10

Brandsdal


I just came across this same problem and wanted to point out that removing the "Handles btnSave.Click" from your event will prevent it from being called twice. For Example, use this in the code behind:

Protected Sub btnSave_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)
    'Do Something
End Sub

Instead of this:

Protected Sub _btnSave_OnClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
    'Do Something
End Sub
like image 24
Mark B Avatar answered Oct 17 '22 05:10

Mark B