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

前端 未结 5 1727

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

相关标签:
5条回答
  • 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...

    0 讨论(0)
  • 2020-12-14 08:47

    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
    }
    
    0 讨论(0)
  • 2020-12-14 08:51

    I know it's late but i lost so much time on this problem. I was setting InnerHtml of a div with buttons. You need to add type="submit" to your button if you want it to work.

    0 讨论(0)
  • 2020-12-14 08:53

    btnLogin.Click += new EventHandler( btnLogin_Click );

    will assign the btnLogin_Click event handler to the button's Click event.

    however, I would point out that assigning a handler in the markup of the aspx page does not "expose your codes", as the HTML rendered down to the client doesn't have any of that information in it.

    0 讨论(0)
  • 2020-12-14 08:55

    It's late but for someone out there: Apply UseSubmitBehavior="False". Example

    <button type="submit" runat="server" UseSubmitBehavior="False" type="button" id="btnLogin" class="button" onclick="btnLogin_Click();">
    
    0 讨论(0)
提交回复
热议问题