asp.net.ph

Skip Navigation LinksHome > Getting Started > ASP.NET Syntax > ASP.NET Syntax for HTML Controls > HtmlInputButton

HtmlInputButton Control Syntax

HtmlInputButton Class   Input Button Element


Creates a push-button control.

Declarative Syntax

For information on the individual members of this class, see HtmlInputButton in the class library.

Syntax Notes

  • This control does not require a closing tag.

Working with HtmlInputButton

The HtmlInputButton control enables programming of the HTML <input type=button>, <input type=submit>, and <input type=reset> elements.

When a user clicks an HTMLInputButton control, input from the HtmlForm where the control is embedded is posted to the server, processed, and a response sent back to the requesting browser.

This control is typically used in conjunction with other user input controls, such as the HtmlInputText, HtmlInputCheckBox, and HtmlSelect controls to gather user input that need to be processed on the server.

HtmlInputButton ( <input type=button> ) functions similarly to HtmlButton ( <button> ), except that it can target any HTML-compliant downlevel browser.

The following samples illustrate use of the HtmlInputButton control.

Responding to an HtmlInputButton Control Event

This sample shows how to respond to an event triggered by a user clicking on an HtmlInputButton.

When the button is clicked, program control is transferred to the button’s click event handler, which in this case simply specifies a message to display.

Responding to an HtmlInputButton Control Event
Run Sample | View Source
  1. In the <body> of the Web Forms document, declare the HtmlInputButton and the element where the message is to be rendered, in this case, a <p runat="server"> control.
    <form runat="server">
    
       <p>The default input type button look: </p>
    
       <p><input type=button id="Button1" runat="server"
          value="Click me!"
          onServerClick="saySomething" />
    
       <p>With rollover effect: </p>
    
       <p><input type=button id="Button2" runat="server"
          value="Click me too!"
          onServerClick="saySomething"
          style="font:13pt verdana; background-color:lightgreen;
             border-color:gray; height=50; width:150; padding:5"
          onMouseOver="this.style.backgroundColor='yellow'"
          onMouseOut="this.style.backgroundColor='lightgreen'" />
    
       <p id="msg" runat="server" />
    
    </form>
  2. In the <head> of the Web Forms page, define the button’s click event handler.
<script language="C#" runat="server">
void saySomething ( object src, EventArgs e ) {
   switch ( ( ( HtmlInputButton ) src ).ID ) {
   case "Button1" :
      msg.InnerHtml = "Say something amusing here <img src='/shared/biggrin.gif'>";
      break;
   case "Button2" :
      msg.InnerHtml = "Say something awesome here <img src='/shared/razz.gif'>";
      break;
   }
}
</script>

Submit and Reset HtmlInputButtons

The HtmlInputButton control also implements the functionality of HTML Submit and Reset button types.

<input type=submit> sends the form for processing, whereas <input type=reset> restores all of the entry fields in a form to their initial values.

This sample illustrates use of submit and reset buttons to process a simple authentication scheme by comparing text entered by the user with text specified in event handling code.

Submit and Reset HtmlInputButtons
Run Sample | View Source
  1. In the <body> of the Web Forms page, declare an HtmlForm control to contain two HtmlInputText controls, two HtmlInputButton controls, and a <p runat="server"> control.
    <form runat="server">
    
       <p>Login: <input id="logIn" runat="server">
    
       <p>Password: <input type=password id="passWord" runat="server">
    
       <p><input type=submit value="Enter" runat="server"
          onServerClick="submitHandler">
    
       <input type=reset runat="server">
    
       <p id="msg" runat="server" />
    
    </form>
  2. In the <head> of the Web Forms page, define the event handler for the submit button.
<script language="C#" runat="server">
void submitHandler ( object Source, EventArgs e ) {
   if ( passWord.Value == "asp.net" ) msg.InnerHtml = "Password is correct";
   else msg.InnerHtml = "That password is not correct";
}
</script>

The submitHandler method in this case simply checks if the entry in the password field is "asp.net", and the server sends back the appropriate response message.

Of course in a real-world scenario, password checking is much more involved, typically matching from an encrypted value in a database.

See Also

HtmlInputButton Class   Input Button Element   Web Forms Events and Handlers



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note