Freigeben über


Button Web Server Controls Overview

Using ASP.NET button Web server controls on ASP.NET Web pages allows users to indicate that they have completed the form or that they want to perform a specific command. Web server controls include three kinds of buttons, each of which appears differently on Web pages.

Types of Buttons

The following table lists the types of buttons that you can create by using Web server controls.

Control Description

Button Web server control

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

LinkButton Web server control

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 Web server control

Allows you to specify a graphic as a button. This is useful for presenting a rich button appearance. The ImageButton controls also pinpoint where in the graphic a user has clicked, which allows you to use the button as an image map.

NoteNote

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.

Button Events

When a user clicks any of the three types of buttons, a form is submitted 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 property

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

ValidationGroup property

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

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 canceling 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. When this is the case, you should typically respond to the button controls' events differently than when the button controls are exclusively used on a form. When a user clicks a button that is in a data control, the event message is sent to the data control, where it 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 the list Web server controls can contain many different 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 the buttons' Text property via data binding.

See Also

Tasks

How to: Add Client Script Events to ASP.NET Web Server Controls

Concepts

Client Script in ASP.NET Web Pages
Cross-Page Posting in ASP.NET Web Pages