Click here to Skip to main content
15,888,340 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
For a button in an asp.net page, I need to click it on the second time to fire the button click event. I believe that some settings may not be right. How can make only single click happen to fire the the event? Thanks.
Posted
Updated 12-Feb-17 18:40pm
Comments
[no name] 31-Mar-14 11:39am    
How would we know what you did to cause this situation when you do not show us your code?
Thanks7872 31-Mar-14 12:06pm    
Did you mean to say the click event code should be fired after second click on the button, that is no action should be taken on first click?
[no name] 31-Mar-14 12:38pm    
show your code first! then make to understand

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" xmlns:asp="#unknown" />

- 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" xmlns:asp="#unknown" />

To this:
<asp:button id="btnTest" runat="server" text="Test" xmlns:asp="#unknown" />

Rebuild, and bingo! - 1 event per click.

ripped from here[^]

-KR
 
Share this answer
 
Comments
[no name] 31-Mar-14 15:06pm    
Thanks for your post. But in my code:
<asp:Button ID="ButtonLogin" runat="server" Text="Login"
onclick="ButtonLogin_Click"
ToolTip="Login" style="text-align: left" CausesValidation="False"
Enabled="False" />
There is no xmlns:asp="#unknown". I will check it again.
Krunal Rohit 31-Mar-14 15:36pm    
Ignore that xmlns.

-KR
[no name] 31-Mar-14 15:23pm    
The problem may be due to another control (e.g. a textbox). For whatever reason, I have to make a click before clicking the button. To eliminate this problem, I revised the code that runs textbox_TextChanged(). Then, clicking the button does not need to do twice any more. Thanks to all of the reviewers.
See my comment on March 14, 2014:
The problem may be due to another control (e.g. a textbox). For whatever reason, I have to make a click before clicking the button. To eliminate this problem, I revised the code that runs textbox_TextChanged(). Then, clicking the button does not need to do twice any more. Thanks to all of the reviewers.
Thanks to Reviewers, esp. to those provided comments or solutions.
 
Share this answer
 
if you use ID variable without initialize, this maybe source of the mistake.
 
Share this answer
 
Quote:
Thank you very much for this tip of yours. After all the other clever advice that led nowhere, this was exactly the simple and reliable solution. So, simply assign an ID to the button, even if you don't need an ID for the button. But just defining the ID of the button solved the problem for me and the click event fired immediately!
 
Share this answer
 
Comments
CHill60 14-Feb-24 8:54am    
We cannot tell who you are thanking - this should have been a comment (using the 'Have a Question or Comment?' link) not a solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900