Export (0) Print
Expand All

Button Web Server Controls Overview

Use the ASP.NET button Web server controls to enable users to post a page to the server and to trigger an event on a page.

This topic contains:

You can use the Button Web server control to provide users the ability to post a page to the server. The control triggers an event in server code that you can handle to respond to the postback. For example, a user can indicate that they have completed a form or that they want to perform a specific command.

ASP.NET includes three kinds of button controls, each of which appears differently on Web pages, as listed in the following table:

Control

Description

Button

Presents a standard command button, which is rendered as an HTML input element.

LinkButton

Renders as a hyperlink in the page. However, it contains client-side script that causes the form to be posted back to the server. (You can create a true hyperlink by using the HyperLink Web server control.)

ImageButton

Renders a graphic as a button. This is useful for presenting a rich button appearance. The ImageButton control also provides information about the coordinates within the graphic where has 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.

The ImageMap control lets you create a graphic that has hotspots that users can click to perform a postback or other action.

Button 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. The buttons can also raise their own Click events, for which you can write event handlers.

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.

Button Postback Behavior

When users click a button control, the page is posted back to the server. By default, the page is posted back to itself, where the same page is regenerated and the event handlers for controls on the page are processed.

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 Pages.

By default, the Button control submits the page by using an HTML POST operation. The LinkButton and ImageButton controls cannot directly support an HTML POST operation. Therefore, when you use those buttons, they add client script to the page that allows the controls to submit the page programmatically. (The LinkButton and ImageButton controls therefore require that client script is enabled on the browser.)

Under some circumstances, you might want the Button control to also use client script to perform the postback. 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.

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.

Handling Client-Side Events for Button Controls

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.

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.

The following table lists the key classes that relate to button controls.

Member

Description

Button, LinkButton, ImageButton

The main classes for button controls.

Back to top

Community Additions

ADD
Show:
© 2015 Microsoft