Search code examples
asp.nethtmlbuttoninputservercontrols

ASP .NET Event Wire-Up for <input type="button"> tag (not asp:button or server link)


Here is what I want to do. Use this HTML line and have the ASP .NET server-side deal with the onclick event.

I don't want to use nor do I want to use an anchor tag which would both allow me to capture the server-side successfully.

I want to use this:

<input type="button" id="submit" name="submit" value="See Your Results" onclick="" />

The onclick attribute for this HTML typically directs the event to a Javascript event handler, which I do not want to use.

This question (http://stackoverflow.com/questions/3445515/input-typebutton-runatserver-wont-work-in-asp-net) details a similar issue, but the end result is to use . I am wondering if it's possible to handle the input tag without resorting to the tag use.

Thanks!


Solution

  • I believe you need to add runat="server".

    Then your onclick will call a server-side method.

    This needs to be in a server form tag (see code below).

    Here's some information about HTML server controls.

    And here's some sample code that you can play with on their webpage:

    Declare two HtmlButton controls in an .aspx file (remember to embed the control inside an HtmlForm control). Next, write an event handler that specifies what to happen when a button is clicked.

    <script  runat="server">
    Sub button1(Source As Object, e As EventArgs)
       p1.InnerHtml="You clicked the blue button!"
    End Sub
    Sub button2(Source As Object, e As EventArgs)
       p1.InnerHtml="You clicked the pink button!"
    End Sub
    </script>
    
    <html>
    <body>
    
    <form runat="server">
    <button id="b1" OnServerClick="button1"
    style="background-color:#e6e6fa;
    height:25;width:100" runat="server">
    Blue button!
    </button>
    <button id="b2"
    OnServerClick="button2"
    style="background-color:#fff0f5;
    height:25;width:100" runat="server">
    Pink button!
    </button>
    <p id="p1" runat="server" />
    </form>
    
    </body>
    </html>