Reputation: 13763
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>
Upvotes: 15
Views: 31881
Reputation: 61
For me the problem was that the function was calling twice, once from the aspx page as:
<asp:Button ID="btnSave" CssClass="button btn btn-primary" runat="server" Text="Save" OnClick="btnSave_Click" CausesValidation="true" />
and one in code behind like:
this.btnSave.Click += new EventHandler(this.btnSave_Click);
removing from one side can solve the problem
Upvotes: 0
Reputation: 629
It is because the button type is submit. You need to change it to button to solved the button events triggering twice. Refer this blog for further explanation.
https://mzulkamal.com/blog/asp-net-web-form-html-button-tag-fires-two-times-s
Upvotes: 0
Reputation: 75
In my case, It happens to me when I use Chrome, But not in Firefox
Upvotes: 0
Reputation: 946
The solution is already been mentioned in different answer . Use type ="button" instead of submit
Here is a sample code
<span class="input-group-btn">
<button id="btnLoadCustomerFile" class="btn btn-default" type="button" runat="server" onserverclick="btnLoadCustomerFile_Click" >Load</button>
</span>
And the backend code to handle the on click
protected void btnLoadCustomerFile_Click(object sender,EventArgs e)
{
LoadCustomerFile();
}
Upvotes: 2
Reputation: 922
Just had to delve back into old school ASPX again and found this issue.
My form was set up like this.
<asp:UpdatePanel ID="login" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="forgotPassword" runat="server" DefaultButton="lbtnSendLoginDetails" Visible="False">
<asp:LinkButton ID="lbtnSendLoginDetails" runat="server" >
The code behind was
Protected Sub lbtnSendLoginDetails_Click(sender As Object, e As EventArgs) Handles lbtnSendLoginDetails.Click
The answer for me was to remove the DefaultButton attribute from the Panel and add
TabIndex="4"
to the LinkButton.
Upvotes: 0
Reputation: 1396
I had same issue, but quite different cause. I setup container panel defaultbutton property to the button firing twice. After removing the "DefaultButton" property from the Asp:Panel the problem is solved.
Upvotes: 0
Reputation: 29
The correct way (in the ASP.NET Web Form Page), as already pointed out by couple of other contributors, is to add the type="button" attribute to the element, and associate a onserverclick event delegate handler.
For example:
<button type="button" id="btnSave" runat="server" onserverclick="btnSave_ServerClick" class="btn btn-success btn-label">
<i class="fa fa-save"></i>Save
</button>
Hope this helps.
Upvotes: 0
Reputation: 1
I think I just figured a solution for it. Remove name="btnSave"
or change it to "btnSave1"
. It will work.
Upvotes: 0
Reputation: 36
If you are using asp.net, just use asp:button.....its clean and simple. No matter what justification i read on the net, it makes no sense that the server side handler should be called twice. When I add type="button", the problem is solved. However the form is not submitted when I press enter key. So that's another issue to be resolved. Using the asp:button takes care of all this.
Upvotes: -1
Reputation: 55
One possible reason:
Check your button declaration in your .aspx source. If you have a 'runat=server'
and onclick="button1_click"
, and you have an event handler in your code-behind (ie. .aspx.vb),
it will cause the event to be fired twice. Here is an example in xxx.aspx:
<asp:Button id="button1" onclick="button1_Click" runat="server" Text="Click Me!"></asp:Button>
This declaration should be:
<asp:Button id="button1" runat="server" Text="Click Me!"></asp:Button>
Good luck!
Upvotes: 1
Reputation: 415
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.
Upvotes: 29
Reputation: 1186
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
Upvotes: 9
Reputation: 7367
If you are using code behind, don't use OnClick in ASP tag, <button id="btnSave" name="btnSave" type="submit" style="font-size:11px;font-family:Verdana;font-weight:bold;" runat="server">Save</button>
Upvotes: 2
Reputation: 4015
Looks like the problem with wiring up of event handlers. http://geekswithblogs.net/TimH/archive/2006/10/23/94874.aspx
Try if you can just have event handler in your back end. Also, the problem could be because of the type = "Submit". Check if changing it to just Button fixes the problem.
Upvotes: 2
Reputation: 14906
Use a native ASP.NET button instead.
<asp:Button id="btnSave" runat="server" OnClick="btnAddUser_Click"
style="font-size:11px;font-family:Verdana;font-weight:bold;" Text="Save" />
Upvotes: 2