See Also: Button Members
In this topic:
Use the System.Web.UI.WebControls.Button control to create a push button on the Web page that lets users to post a page to the server. The control triggers an event in server code that you can handle to respond to the postback. It can also raise an event in client script that you can handle before the page is posted or that can run and then cancel submission of the page.
ASP.NET includes several kinds of button controls, each of which appears differently on Web pages. They are the System.Web.UI.WebControls.Button control, which renders as a push button; the System.Web.UI.WebControls.LinkButton control, which renders as a link; and the System.Web.UI.WebControls.ImageButton control, which renders as an image; and the System.Web.UI.WebControls.ImageMap control, which lets you create a graphic that has hotspots that users can click. By default, all button controls submit the page when clicked. You can also use the System.Web.UI.HtmlControls.HtmlButton and System.Web.UI.HtmlControls.HtmlInputButton controls to create buttons on the page that are programmable in server code. For details about the differences between HTML and Web server controls, see Introduction to ASP.NET Web Server Controls.
By default, a System.Web.UI.WebControls.Button control is a Submit button. A Submit button does not have a command name (specified by the Button.CommandName property) associated with the button and simply posts the Web page back to the server. You can provide an event handler for the Button.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 Button.CommandName property. This allows you to create multiple System.Web.UI.WebControls.Button controls on a Web page and programmatically determine which System.Web.UI.WebControls.Button control is clicked. You can also use the Button.CommandArgument property with a command button to provide additional information about the command to perform, such as Ascending. You can provide an event handler for the Button.Command event to programmatically control the actions performed when the Command button is clicked.
When a user clicks any Web server control button, the page is sent to the server. This causes the Web page to be processed and any pending events to be raised in server-based code. When all page and control processing has finished, the page renders itself to the browser again.
When users click a button control, the page is posted back to the server. By default, the page is posted back to itself,
You can configure buttons to post the current page to another page. This can be useful for creating multi-page forms. For details, see Cross-Page Posting in an ASP.NET Web Page.
You might also want the System.Web.UI.WebControls.Button control to also use client script to perform the postback (instead of simply performing an HTTP POST operation). This can be useful if you want to programmatically manipulate the postback, such as attaching it to other elements on the page. You can set the System.Web.UI.WebControls.Button control's Button.UseSubmitBehavior property to true to cause the System.Web.UI.WebControls.Button control to use client-script based postback.
If a page contains ASP.NET validator controls, by default, clicking a button control causes the validator control to perform its check. If client-side validation is enabled for a validator control, the page is not submitted if a validation check has failed.
The following table describes the properties supported by button controls that enable you to control the validation process more precisely.
Specifies whether clicking the button also performs a validation check. Set this property to false to prevent a validation check.
Enables you to specify which validators on the page are called when the button is clicked. If no validation groups are established, a button click calls all of the validators that are on the page.
For more information, see Introduction to Validating User Input in ASP.NET Web Pages.
Button Web server controls are often used in data controls, such as in the System.Web.UI.WebControls.DataList, System.Web.UI.WebControls.GridView, and System.Web.UI.WebControls.Repeater list controls. In those cases, you typically do not respond directly to the button click event. Instead, a button in a data control raises an event that is specific to the data control. For example, in the System.Web.UI.WebControls.DataList control, a button might raise the System.Web.UI.WebControls.DataList control's DataList.ItemCommand event instead of raising the System.Web.UI.WebControls.Button control's Button.Click event.
Because data-bound list controls can contain many buttons, you can set the button's Button.CommandArgument property to specify a value to pass as part of the event. You can then test for this argument to see which button was clicked.
You can bind the button Web server controls to a data source in order to control their property settings dynamically. For example, you can set a button's Button.Text property by using data binding.
Partial-page rendering makes it possible to refresh portions of a page without a postback. System.Web.UI.UpdatePanel controls enable you to mark parts of the page that participate in partial-page rendering. By default, the behavior of controls inside an System.Web.UI.UpdatePanel control, including System.Web.UI.WebControls.Button controls, is to perform an asynchronous postback instead of a postback. This refreshes only the contents of the System.Web.UI.UpdatePanel control from which the postback originates.
In addition to the scenario of a System.Web.UI.WebControls.Button control that is inside an System.Web.UI.UpdatePanel control, you can use System.Web.UI.WebControls.Button controls with System.Web.UI.UpdatePanel controls in the following scenarios:
Defining a System.Web.UI.WebControls.Button control that is outside an System.Web.UI.UpdatePanel control as an System.Web.UI.AsyncPostBackTrigger control for that panel. When the button is clicked, it performs an asynchronous postback and refreshes the contents of the panel.
Defining a System.Web.UI.WebControls.Button control that is inside an System.Web.UI.UpdatePanel control as a System.Web.UI.PostBackTrigger for the panel. When the button is clicked, it performs a postback even though it is inside an System.Web.UI.UpdatePanel control.
<asp:Button AccessKey="string" BackColor="color name|#dddddd" BorderColor="color name|#dddddd" BorderStyle="