Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET/HTML: HTML button's onClick property inside ASP.NET (.cs)

Tags:

I just wanna find out if there's a way to put my onClick event inside .cs:

<button type="submit" runat="server" id="btnLogin" class="button" onclick="btnLogin_Click();"> 

where Login_Click() should be inside .cs:

protected void btnLogin_Click(object sender, EventArgs e) {     // do something }  

Please do take note that I will not use ASP.NET button here, and that I will not put my Login_Click() event inside .html/.aspx so I can't 'expose' my codes. Any suggestions?

like image 875
abramlimpin Avatar asked Jul 02 '10 15:07

abramlimpin


2 Answers

You can do that on any server control and this button is made a server control by defining "runat=server". The problem is probably in your definition of the event:

<button ... runat="server" ... onServerClick="btnLogin_Click" /> 

You don't need "();" there...

Apart from that can you explain why you don't use the <asp:Button> here because I don't really see a problem with that...

like image 53
Koen Avatar answered Oct 18 '22 06:10

Koen


You'll want to use the onServerClick. There's an example of how to do that on MSDN:

<button id="Button1" OnServerClick="Button1_OnClick" runat="server">     Click me! </button>   protected void Button1_OnClick(object Source, EventArgs e) {     // secret codes go here } 
like image 24
Roman Avatar answered Oct 18 '22 05:10

Roman