Button Class

Definition

Displays a push button control on the Web page.

public class Button : System.Web.UI.WebControls.WebControl, System.Web.UI.IPostBackEventHandler, System.Web.UI.WebControls.IButtonControl
Inheritance
Implements

Inherited Members

System.Object

System.Web.UI.Control

System.Web.UI.WebControls.WebControl

Examples

The following code example demonstrates how to create a Submit Button control that posts the Web page content back to the server.

The following code example demonstrates how to create a Command Button control that sorts a list.

Remarks

In this topic:

Introduction

Use the 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 Button control, which renders as a push button; the LinkButton control, which renders as a link; and the ImageButton control, which renders as an image; and the 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 HtmlButton and 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 ASP.NET Web Server Controls Overview.

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 page and programmatically determine which Button control is clicked. You can also use the 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 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 Click events or 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 Forms 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 ASP.NET Web Forms.

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 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 Button control's UseSubmitBehavior property to true to cause the 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.

Property Description
CausesValidation Specifies whether clicking the button also performs a validation check. Set this property to false to prevent a validation check.
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 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 DataList, GridView, and 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 DataList control, a button might raise the DataList control's ItemCommand event instead of raising the Button control's Click event.

Because data-bound list controls can contain many buttons, you can set the button's 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 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. UpdatePanel controls enable you to mark parts of the page that participate in partial-page rendering. By default, the behavior of controls inside an UpdatePanel control, including Button controls, is to perform an asynchronous postback instead of a postback. This refreshes only the contents of the UpdatePanel control from which the postback originates.

In addition to the scenario of a Button control that is inside an UpdatePanel control, you can use Button controls with UpdatePanel controls in the following scenarios:

  • Defining a Button control that is outside an UpdatePanel control as an AsyncPostBackTrigger control for that panel. When the button is clicked, it performs an asynchronous postback and refreshes the contents of the panel.

  • Defining a Button control that is inside an UpdatePanel control as a PostBackTrigger for the panel. When the button is clicked, it performs a postback even though it is inside an UpdatePanel control.

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

Declarative Syntax

<asp:Button  
    AccessKey="string"  
    BackColor="color name|#dddddd"  
    BorderColor="color name|#dddddd"  
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|  
        Inset|Outset"  
    BorderWidth="size"  
    CausesValidation="True|False"  
    CommandArgument="string"  
    CommandName="string"  
    CssClass="string"  
    Enabled="True|False"  
    EnableTheming="True|False"  
    EnableViewState="True|False"  
    Font-Bold="True|False"  
    Font-Italic="True|False"  
    Font-Names="string"  
    Font-Overline="True|False"  
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|  
        Large|X-Large|XX-Large"  
    Font-Strikeout="True|False"  
    Font-Underline="True|False"  
    ForeColor="color name|#dddddd"  
    Height="size"  
    ID="string"  
    OnClick="Click event handler"  
    OnClientClick="string"  
    OnCommand="Command event handler"  
    OnDataBinding="DataBinding event handler"  
    OnDisposed="Disposed event handler"  
    OnInit="Init event handler"  
    OnLoad="Load event handler"  
    OnPreRender="PreRender event handler"  
    OnUnload="Unload event handler"  
    PostBackUrl="uri"  
    runat="server"  
    SkinID="string"  
    Style="string"  
    TabIndex="integer"  
    Text="string"  
    ToolTip="string"  
    UseSubmitBehavior="True|False"  
    ValidationGroup="string"  
    Visible="True|False"  
    Width="size"  
/>  

Constructors

Button()

Initializes a new instance of the Button class.

Properties

CausesValidation

Gets or sets a value indicating whether validation is performed when the Button control is clicked.

CommandArgument

Gets or sets an optional parameter passed to the Command event along with the associated CommandName.

CommandName

Gets or sets the command name associated with the Button control that is passed to the Command event.

OnClientClick

Gets or sets the client-side script that executes when a Button control's Click event is raised.

PostBackUrl

Gets or sets the URL of the page to post to from the current page when the Button control is clicked.

Text

Gets or sets the text caption displayed in the Button control.

UseSubmitBehavior

Gets or sets a value indicating whether the Button control uses the client browser's submit mechanism or the ASP.NET postback mechanism.

ValidationGroup

Gets or sets the group of controls for which the Button control causes validation when it posts back to the server.

Methods

AddAttributesToRender(HtmlTextWriter)

Adds the attributes of the Button control to the output stream for rendering on the client.

GetPostBackOptions()

Creates a PostBackOptions object that represents the Button control's postback behavior.

OnClick(EventArgs)

Raises the Click event of the Button control.

OnCommand(CommandEventArgs)

Raises the Command event of the Button control.

OnPreRender(EventArgs)

Determines whether the button has been clicked prior to rendering on the client.

RaisePostBackEvent(String)

Raises events for the Button control when it posts back to the server.

RenderContents(HtmlTextWriter)

Renders the contents of the control to the specified writer.

Events

Click

Occurs when the Button control is clicked.

Command

Occurs when the Button control is clicked.

Explicit Interface Implementations

IPostBackEventHandler.RaisePostBackEvent(String)

Raises events for the Button control when it posts back to the server.

Extension Methods

FindDataSourceControl(Control)
FindFieldTemplate(Control, String)
FindMetaTable(Control)