System.Web.UI.WebControls.Button Class

Displays a push button control on the Web page.

See Also: Button Members

Syntax

[System.Web.UI.SupportsEventValidation]
[System.Web.UI.ToolboxData("<{0}:Button runat="server" Text="Button"></{0}:Button>")]
[System.ComponentModel.Designer("System.Web.UI.Design.WebControls.ButtonDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
[System.ComponentModel.DefaultProperty("Text")]
[System.Web.UI.DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.ComponentModel.DefaultEvent("Click")]
public class Button : WebControl, System.Web.UI.IPostBackEventHandler, IButtonControl

Remarks

In this topic:

Introduction

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.

Note:

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.

Button Postback Behavior and Server Events

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.

The buttons can raise their own Button.Click events or Button.Command event, which you can handle by using server-based code. This is different than events in traditional HTML pages or in client-based Web applications, where a button's onclick event is handled by using JavaScript that runs in the client. For more information, see ASP.NET Web Server Control Event Model.

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.

Handling Button Events in Client Script

Button controls can raise both server events and client events. Server events occur after postback, and they are handled in the server-side code that you write for the page. Client events are handled in client script, typically ECMAScript (JavaScript), and are raised before the page is submitted. By adding client-side events to ASP.NET button controls, you can perform tasks such as displaying confirmation dialog boxes before submitting the page, and potentially cancel the submission. For details, see Client Script in ASP.NET Web Pages and How to: Respond to Button Web Server Control Events in Client Script.

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.

Button Controls and Validation

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.

Button.CausesValidation

Specifies whether clicking the button also performs a validation check. Set this property to false to prevent a validation check.

Button.ValidationGroup

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.

Buttons in Data Controls

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.

Binding Data to the Controls

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.

Using Buttons with UpdatePanel Controls

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:

For more information about partial-page rendering and using System.Web.UI.UpdatePanel controls, see UpdatePanel Control Overview and Partial-Page Rendering Overview.

Declarative Syntax

Example

<asp:Button
    AccessKey="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="

Requirements

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0