GridView CommandField
GridView is an ASP.NET web server control. Developers can manually
specify which field appears in GridView by setting the AutoGenerateColumns
property value to False. They can define their own column field collection and
column field types. GridView supports different column field types, these are
BoundField, ButtonField, CheckBoxField, CommandField, HyperLinkField,
ImageField, and TemplateField.
CommandField represents a special field that displays command buttons to perform selecting, editing, or deleting operations in GridView. GridView server control displays a CommandField as a column.
CommandField's ShowSelectButton property allows us to show or hide a Select button in a CommandField field for each record in GridView. This Select button allows us to select a record in the GridView server control.
ShowEditButton property provides a way to show or hide an Edit button in a CommandField field for each record in GridView. This Edit button allows us to edit a record from the data source. If we click an Edit button for a specific record, the edit button is replaced with an Update button and a Cancel button, all other Command buttons are also hidden.
ShowDeleteButton property allows us to show or hide a Delete button in a CommandField field for each record in GridView. this Delete button allows us to delete a record from the data source.
SelectImageUrl, EditImageUrl, DeleteImageUrl, UpdateImageUrl, and CancelImageUrl properties allow us to display the image as a button instead Select, Edit, Delete, Update, and Cancel default button. To apply an image button instead default command button we also need to set the ByttonType property value to Image.
Developers can change the default command button text by setting the following properties those are SelectText, EditText, DeleteText, CancelText, and UpdateText property.
The following ASP.NET C# example code demonstrates to us how can we use CommandField in GridView server control.
CommandField represents a special field that displays command buttons to perform selecting, editing, or deleting operations in GridView. GridView server control displays a CommandField as a column.
CommandField's ShowSelectButton property allows us to show or hide a Select button in a CommandField field for each record in GridView. This Select button allows us to select a record in the GridView server control.
ShowEditButton property provides a way to show or hide an Edit button in a CommandField field for each record in GridView. This Edit button allows us to edit a record from the data source. If we click an Edit button for a specific record, the edit button is replaced with an Update button and a Cancel button, all other Command buttons are also hidden.
ShowDeleteButton property allows us to show or hide a Delete button in a CommandField field for each record in GridView. this Delete button allows us to delete a record from the data source.
SelectImageUrl, EditImageUrl, DeleteImageUrl, UpdateImageUrl, and CancelImageUrl properties allow us to display the image as a button instead Select, Edit, Delete, Update, and Cancel default button. To apply an image button instead default command button we also need to set the ByttonType property value to Image.
Developers can change the default command button text by setting the following properties those are SelectText, EditText, DeleteText, CancelText, and UpdateText property.
The following ASP.NET C# example code demonstrates to us how can we use CommandField in GridView server control.
GridViewCommandFieldExample.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net GridView CommandField, SelectedRowStyle example: how to use CommandField</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy">GridView CommandField, SelectedRowStyle Example</h2>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT CategoryID, CategoryName, Description FROM Categories"
>
</asp:SqlDataSource>
<asp:GridView
ID="GridView1"
runat="server"
DataSourceID="SqlDataSource1"
ForeColor="AliceBlue"
BackColor="DarkSalmon"
BorderColor="Salmon"
HeaderStyle-BackColor="Crimson"
AllowPaging="true"
AutoGenerateColumns="false"
DataKeyNames="CategoryID"
AutoGenerateSelectButton="false"
>
<Columns>
<asp:BoundField HeaderText="Category ID" DataField="CategoryID" />
<asp:BoundField HeaderText="Category Name" DataField="CategoryName" />
<asp:BoundField HeaderText="Description" DataField="Description" />
<asp:CommandField ShowSelectButton="true" ButtonType="Link" SelectText="Select" />
</Columns>
<SelectedRowStyle BackColor="LightPink" Font-Italic="true" ForeColor="Crimson" />
</asp:GridView>
</div>
</form>
</body>
</html>