Button Web Server Control

Displays a push button control on the Web Forms page.

<asp:Button id="MyButton"
     Text="label"
     CommandName="command"
     CommandArgument="commandargument"
     CausesValidation="true | false"
     OnClick="OnClickMethod"
     runat="server"/>

Remarks

The Button control allows you to create a push button on the Web Forms page. There are two types of buttons that can be created. You can create either a submit button or a command button.

By default, a Button control is a submit button. A submit button does not have a command name (specified by the CommandName property) associated with the button and simply posts the Web page back to the server. You can provide an event handler for the Click event to programmatically control the actions performed when the submit button is clicked.

A command button has a command name associated with the button (such as Sort) by setting the CommandName property. This allows you to create multiple Button controls on a Web Forms page and programmatically determine which Button control is clicked in the event handler for the Command event. You can also use the CommandArgument property with a command button to provide addition information about the command to perform, such as Ascending. You can provide an event handler for the Command event to programmatically control the actions performed when the command button is clicked.

By default, page validation is performed when a Button control is clicked. Page validation determines whether the input controls associated with a validation control on the page pass the validation rules specified by the validation control. If you have a Button control that needs to disable this behavior, such as a reset button, set the CausesValidation property to false.

Note   Because the <asp:Button> element has no content, you can close the tag with /> instead of a separate closing tag.

To specify the caption that is displayed in the Button control, set the Text property.

For detailed information on the Button control's properties and events, see the Button Class documentation.

Example

The following example demonstrates the declaration for a submit button control in an .aspx file.

<asp:Button id="SubmitButton"
     Text="Submit"
     OnClick="SubmitBtn_Click"
     runat="server"/>

The following example demonstrates the declaration for a command button control in an .aspx file.

<asp:Button id="SortAscendingButton"
     Text="Sort Ascending"
     CommandName="Sort"
     CommandArgument="Ascending"
     OnCommand="CommandBtn_Click"
     runat="server"/>

The following example shows an event-handling method that gets the button click and displays the information passed from the button in its CommandName and CommandArgument properties.

Sub CommandBtn_Click(sender As Object, e As CommandEventArgs) 
   Message.Text = "You clicked the " & e.CommandName & _
                  " - " & e.CommandArgument & " button."
End Sub
[C#]
void CommandBtn_Click(Object sender, CommandEventArgs e) 
{
   Message.Text = "You clicked the " + e.CommandName +
                  " - " + e.CommandArgument + " button.";
}

See Also

Web Server Controls | Button Class