asp.net.ph

Skip Navigation Links

Adding Button Fields to a GridView Control

Controls You Can Use on Web Forms   ASP.NET Data Controls   GridView Control


A ButtonField in a GridView control enables authors to define custom functionality for items in the grid beyond the select, edit, delete, and hyperlink features of other column types. Using the ButtonField, an author can include custom-defined buttons, such as an "Add to shopping cart" button.

A ButtonField can have one of the following button types:

Button Type Description
Link Renders a column of link buttons.
Button Renders a column of push buttons.
Image Renders a column of buttons with images.

The button captions can either be static text or text read from a database.

To add a button column

  1. Set the GridView control’s AutoGenerateColumns property to false.
  2. Optionally assign the handler ( if there is one ) that will receive control when any of the buttons in the ButtonField is clicked ( in effect, when the GridView's RowCommand event occurs ).
    <asp:GridView id="myGrid" runat="server"
       autogeneratecolumns=false
       ...
       onRowCommand="updateCart"
    >
  3. Within the GridView declaration, declare a <Columns> element.
  4. Within the Columns element, define each of the <asp:ButtonField> controls you intend to display, specifying at the very least the following:
    1. the ButtonType property ( optional for link buttons, required for push and image buttons ).
    2. the CommandName for each button ( required for all button types ).
    3. the Text or DataTextField property ( required for link or push buttons ).
    4. the ImageUrl property ( required for image buttons ).
  5. Optionally set the ButtonField control’s other properties. For syntax, see GridView Control Syntax.

The following examples show several ways of using a ButtonField to display a column of command buttons in a GridView control.

ButtonField ButtonType Example
Run Sample | View Source
Master~Details Using GridViews
Run Sample | View Source
Dynamically Generated ButtonField ImageButtons
Run Sample | View Source

For additional information, see ButtonField in the class library.

See Also

Adding Bound Fields to a GridView Control   Adding Hyperlink Fields to a GridView Control   Adding Template Fields to a GridView Control



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note