See Also: ButtonField Members
The System.Web.UI.WebControls.ButtonField class is used by data-bound controls (such as System.Web.UI.WebControls.GridView and System.Web.UI.WebControls.DetailsView) to display a button for each record that is displayed. The System.Web.UI.WebControls.ButtonField object is displayed differently depending on the data-bound control in which it is used. For example, the System.Web.UI.WebControls.GridView control displays a System.Web.UI.WebControls.ButtonField object as a column, while the System.Web.UI.WebControls.DetailsView control displays it as a row.
Clicking a button in a button field raises the command event of the parent data-bound control. You can provide a custom routine to perform when a command button is clicked by providing an event handler for the command event.
The System.Web.UI.WebControls.GridView control raises the GridView.RowCommand event, while the System.Web.UI.WebControls.DetailsView control raises the DetailsView.ItemCommand event.
To determine the index of the record that raises the command event, use the CommandEventArgs.CommandArgument property of the event argument that is passed to the command event for the data-bound control. The System.Web.UI.WebControls.ButtonField class automatically populates the CommandEventArgs.CommandArgument property with the appropriate index value.
To specify the type of button to display, use the ButtonFieldBase.ButtonType property. When displaying a link or command button, use the ButtonField.Text property to specify the caption to display in the buttons.
If you set the ButtonField.Text property, all buttons in a System.Web.UI.WebControls.ButtonField share the same caption.
Alternatively, you can bind the System.Web.UI.WebControls.ButtonField object to a field in a data source. This allows you to display different captions for the buttons in the System.Web.UI.WebControls.ButtonField object. The values that are in the specified field are used for the text captions of the buttons. Set the ButtonField.DataTextField property to bind a System.Web.UI.WebControls.ButtonField object to a field in a data source.
When displaying an image button, use the ButtonField.ImageUrl property to specify the image to display for the buttons in the System.Web.UI.WebControls.ButtonField object.
All buttons in a System.Web.UI.WebControls.ButtonField object share the same image.
You can hide a System.Web.UI.WebControls.ButtonField object in a data-bound control by setting the DataControlField.Visible property to false.
The System.Web.UI.WebControls.ButtonField object allows you to customize its header and footer sections. To display a caption in the header or footer section, set the DataControlField.HeaderText or DataControlField.FooterText properties, respectively. Instead of displaying text in the header section, you can display an image by setting the DataControlField.HeaderImageUrl property. To hide the header section in a System.Web.UI.WebControls.ButtonField object, set the ButtonFieldBase.ShowHeader property to false.
Some data-bound controls (such as the System.Web.UI.WebControls.GridView control) can show or hide only the entire header section of the control. These data-bound controls do not support the ButtonFieldBase.ShowHeader property for an individual button field. To show or hide the entire header section of a data-bound control (if available), use the ShowHeader property for the control.
You also can customize the appearance of the System.Web.UI.WebControls.ButtonField object (font color, background color, and so on) by setting the style properties for the different parts of the field. The following table lists the different style properties.
DataControlField.ControlStyle |
The child Web server controls of the System.Web.UI.WebControls.ButtonField. |
DataControlField.FooterStyle |
The footer section of the System.Web.UI.WebControls.ButtonField. |
DataControlField.HeaderStyle |
The header section of the System.Web.UI.WebControls.ButtonField. |
DataControlField.ItemStyle |
The data items in the System.Web.UI.WebControls.ButtonField. |