System.Web 1.0.5000.0 2.0.0.0 Gtk# is thread aware, but not thread safe; See the <link location="node:gtk-sharp/programming/threads">Gtk# Thread Programming</link> for details. System.Web.UI.WebControls.WebControl System.Web.UI.IPostBackEventHandler System.Web.UI.WebControls.IButtonControl 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")

In this topic:

Introduction

Button Postback Behavior and Server Events

Handling Button Events in Client Script

Button Controls and Validation

Buttons in Data Controls

Binding Data to the Controls

Using Buttons with UpdatePanel Controls

Declarative Syntax

## Introduction

Use the 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 control, which renders as a push button; the control, which renders as a link; and the control, which renders as an image; and the 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 and 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 control is a Submit button. A Submit button does not have a command name (specified by the property) associated with the button and simply posts the Web page back to the server. You can provide an event handler for the 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 property. This allows you to create multiple controls on a Web page and programmatically determine which control is clicked. You can also use the 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 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 events or 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 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 control's property to true to cause the 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

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.

## Buttons in Data Controls

Button Web server controls are often used in data controls, such as in the , , and 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 control, a button might raise the control's event instead of raising the control's event.

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

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

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

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

For more information about partial-page rendering and using 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" />`

Displays a push button control on the Web page.

- Constructor

  Use this constructor to create and initialize a new instance of the class.

  Initializes a new instance of the class.

  1.0.5000.0
  2.0.0.0
- Method
  System.Void `` To be added

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

  An that contains the output stream to render on the client. 1.0.5000.0 2.0.0.0
- Property
  System.Boolean To be added: an object of type 'bool'

  By default, page validation is performed when a control is clicked. Page validation determines whether the input controls associated with a validation control on the page all pass the validation rules specified by the validation control.

  You can specify or determine whether validation is performed on both the client and the server when a control is clicked by using the property. To prevent validation from being performed, set the property to false.

  You should set the property to false when you are using the property to post back to a different page. You should explicitly check validation when posting back to a different page. For an example, see the Remarks section of the property.

  This property is commonly set to false for a reset or clear button to prevent validation from being performed when the button is clicked.

  When the value of the property is set to true, you can also use the property to specify the name of the validation group for which the control causes validation.

  This property cannot be set by themes or style sheet themes. For more information, see and ASP.NET Themes Overview.

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

  1.0.5000.0
  2.0.0.0
  System.Web.UI.Themeable(false)
  System.ComponentModel.DefaultValue(true)
- Event
  System.EventHandler

  The event is raised when the control is clicked. This event is commonly used when no command name is associated with the control (for instance, with a Submit button).

  For more information about handling events, see Consuming Events.

  Occurs when the control is clicked.

  1.0.5000.0
  2.0.0.0
- Event
  System.Web.UI.WebControls.CommandEventHandler

  The event is raised when the control is clicked. This event is commonly used when a command name, such as Sort, is associated with the control. This allows you to create multiple controls on a Web page and programmatically determine which control is clicked.

  The event is raised through the control hierarchy in the form of the BubbleEvent.

  For more information about handling events, see Consuming Events.

  Occurs when the control is clicked.

  1.0.5000.0
  2.0.0.0
- Property
  System.String To be added: an object of type 'string'

  Use the property to specify a parameter that compliments the property.

  Although you can set the property by itself, it is normally only used when the property is also set.

  The property complements the property by allowing you to provide additional information about the command to perform. For example, if you set the property to Sort and the property to Ascending, you specify a command to sort in ascending order.

  This property cannot be set by themes or style sheet themes. For more information, see and ASP.NET Themes Overview.

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

  1.0.5000.0
  2.0.0.0
  System.Web.UI.Themeable(false)
  System.ComponentModel.Bindable(true)
  System.ComponentModel.DefaultValue("")
- Property
  System.String To be added: an object of type 'string'

  When you have multiple controls on a Web page, use the property to specify or determine the command name associated with each control. You can set the property with any string that identifies the command to perform. You can then programmatically determine the command name of the control and perform the appropriate actions.

  Additional information about the command, such as Ascending, may be specified by using the property.

  This property cannot be set by themes or style sheet themes. For more information, see and ASP.NET Themes Overview.

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

  1.0.5000.0
  2.0.0.0
  System.Web.UI.Themeable(false)
  System.ComponentModel.DefaultValue("")
- Method
  2.0.0.0
  System.Web.UI.PostBackOptions

  The method creates a object that represents the control's postback behavior. The object is then typically passed to the method, which is used to obtain a reference to a client-side script function that, when invoked, causes the server to post back to the page.

  This method is used primarily by control developers when extending the class.

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

  A that represents the control's postback behavior.

- Method
  System.Void ``

  The event is raised when the control is clicked. This event is commonly used when no command name is associated with the control (for instance, with a Submit button).

  Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

  The method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

  Raises the event of the control.

  The event data. 1.0.5000.0 2.0.0.0
- Property
  2.0.0.0
  System.ComponentModel.DefaultValue("")
  System.Web.UI.Themeable(false)
  System.String To be added.

  Use the property to specify additional client-side script that executes when a control's event is raised. The script that you specify for this property is rendered in the control's OnClick attribute in addition to the control's predefined client-side script.

  This property cannot be set by themes or style sheet themes. For more information, see and ASP.NET Themes Overview.

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

- Method
  System.Void ``

  The event is raised when the control is clicked. This event is commonly used when a command name, such as Sort, is associated with the control. This allows you to create multiple controls on a Web page and programmatically determine which control is clicked.

  The event is raised through the control hierarchy in the form of the BubbleEvent.

  Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

  The method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

  Raises the event of the control.

  The event data. 1.0.5000.0 2.0.0.0
- Method
  2.0.0.0
  System.Void ``

  The method is used primarily by control developers when deriving a custom class from the control.

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

  The event data.
- Property
  2.0.0.0
  System.Web.UI.UrlProperty("*.aspx")
  System.Web.UI.Themeable(false)
  System.ComponentModel.Editor("System.Web.UI.Design.UrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
  System.ComponentModel.DefaultValue("")
  System.String To be added.

  The property allows you to perform a cross-page post using the control.

  Only correctly specified paths work with this property. For example, relative paths (Test/default.aspx), absolute paths (https://localhost/WebApp/default.aspx) and virtual (~\Test\default.aspx) work correctly. Incorrectly formed paths such as "/Test/default.aspx" or "\Test\default.aspx" do not work. See ASP.NET Web Site Paths for a discussion on creating correct paths.

  Set the property to the URL of the Web page to post to when the control is clicked. For example, specifying Page2.aspx causes the page that contains the control to post to Page2.aspx. If you do not specify a value for the property, the page posts back to itself.

  When performing a cross-page postback with controls with server-side validation, you should check that the page's property is true before processing the postback. In the case of a cross-page postback, the page to check is the . The following VB code shows how this is done:

  `Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Page.PreviousPage.IsValid Then ' Handle the post back Else Response.Write("Invalid") End If End Sub`

  For more information on cross-page posting techniques, see Cross-Page Posting in an ASP.NET Web Page.

  This property cannot be set by themes or style sheet themes. For more information, see and ASP.NET Themes Overview.

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

- Method
  2.0.0.0
  System.Void ``

  This event is used internally by the control, when it is clicked, to raise a event, and if defined, a event.

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

  The argument for the event.
- Method
  System.Void `` To be added

  Renders the contents of the control to the specified writer.

  A object that represents the output stream to render HTML content on the client. 1.0.5000.0 2.0.0.0
- Method
  1.0.5000.0
  2.0.0.0
  System.Void ``

  This event is used internally by the control, when it is clicked, to raise a event, and if defined, a event.

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

  The argument for the event.
- Property
  System.String To be added: an object of type 'string'

  Use the property to specify or determine the caption to display in the control.

  The value of this property, when set, can be saved automatically to a resource file by using a designer tool. For more information, see and ASP.NET Globalization and Localization.

  Gets or sets the text caption displayed in the control.

  1.0.5000.0
  2.0.0.0
  System.ComponentModel.Localizable(true)
  System.ComponentModel.Bindable(true)
  System.ComponentModel.DefaultValue("")
- Property
  2.0.0.0
  System.Web.UI.Themeable(false)
  System.ComponentModel.DefaultValue(true)
  System.Boolean To be added.

  Use the property to specify whether a control uses the client browser's submit mechanism or the ASP.NET postback mechanism. By default the value of this property is true, causing the control to use the browser's submit mechanism. If you specify false, the ASP.NET page framework adds client-side script to the page to post the form to the server.

  When the property is false, control developers can use the method to return the client postback event for the . The string returned by the method contains the text of the client-side function call and can be inserted into a client-side event handler.

  This property cannot be set by themes or style sheet themes. For more information, see and ASP.NET Themes Overview.

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

- Property
  2.0.0.0
  System.Web.UI.Themeable(false)
  System.ComponentModel.DefaultValue("")
  System.String To be added.

  Validation groups allow you to assign validation controls on a page to a specific category. Each validation group can be validated independently of other validation groups on the page. Use the property to specify the name of the validation group for which the control causes validation when it posts back to the server.

  This property has an effect only when the value of the property is set to true. When you specify a value for the property, only the validation controls that are part of the specified group are validated when the control posts back to the server. If you do not specify a value for this property and the property is set to true, all validation controls on the page that are not assigned to a validation group are validated when the control posts back to the server.

  This property cannot be set by themes or style sheet themes. For more information, see and ASP.NET Themes Overview.

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