简体   繁体   中英

HTML button onserverclick event doesn't work

I'm trying to use the onclickserver on ASP.net to do something when the user clicks on a button but when I click the button nothing happens.

I really can't see what I'm doing wrong.

Here is the button:

<button id="BttnLead" class="bttnBlck" runat="server" onserverclick="BtnLead_OnClick">Lead</button>

And here the event I'm trying to use:

  protected void BtnLead_OnClick(object sender, EventArgs e){}

Thank you.

I would bet it has to do with failed validation somewhere on the page, as @kman pointed out in a comment above. Are you using any <asp:FieldValidator/> controls anywhere on the page? If so, and they're not being handled correctly, all you would need to do to cause this button's postback to be triggered is to add the CausesValidation="false" property to the <button> control. This would have the button avoid the validation code that is (if it's the issue, which I really think it is) inevitably failing and thus never reaching the handler method.

PS It should be noted that you do NOT have to use an ASP.NET control (ie <asp:Button> ) and the pure HTML <button> control with the runat="server" property renders a server side control just the same. However, if you're in the ASP.NET world anyway and you have access to these controls, they do provide some benefit that is nice; but that's a different conversation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM