Home > Getting Started > ASP.NET Syntax > ASP.NET Syntax for HTML Controls > HtmlInputButton
HtmlInputButton Class Input Button Element
Creates a push-button control.
Declarative Syntax
<input id="accessID" runat="server"
type = "button | submit | reset"
value = "buttonText"
onServerClick = "onServerClickHandler"
>
For information on the individual members of this class, see HtmlInputButton in the class library.
- This control does not require a closing tag.
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.
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.
- 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>
- 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>
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.
- 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>
- 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.
HtmlInputButton Class Input Button Element Web Forms Events and Handlers