Export (0) Print
Expand All
Expand Minimize

GridView.AutoGenerateSelectButton Property

Gets or sets a value indicating whether a CommandField field column with a Select button for each data row is automatically added to a GridView control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public virtual bool AutoGenerateSelectButton { get; set; }
<asp:GridView AutoGenerateSelectButton="True|False" />

Property Value

Type: System.Boolean
true to automatically add a CommandField field column with a Select button for each data row; otherwise, false. The default is false.

When the AutoGenerateSelectButton property is set to true, a column (represented by a CommandField object) with a Select button for each data row is automatically added to the GridView control. Clicking the Select button for a row selects that row in the control, which sets the SelectedIndex property to the index of the row. To retrieve the GridViewRow object that represents the selected row, use the SelectedRow property. You can also get the primary key value for the selected record by using the SelectedValue property. The SelectedValue property contains the values of the key fields specified in the DataKeyNames property.

NoteNote

You can programmatically select a row by setting the SelectedIndex property. To cancel the selection of a row, set the SelectedIndex property to -1.

You can control the appearance of the selected row by using the SelectedRowStyle property. Common settings usually include a custom background color, foreground color, and font properties.

The GridView control provides several events that you can use to perform a custom action when a row is selected. The following table lists the available events.

Event

Description

SelectedIndexChanged

Occurs when a row's Select button is clicked, but after the GridView control handles the select operation. This event is often used to perform a task after a row is selected in the control.

SelectedIndexChanging

Occurs when a row's Select button is clicked, but before the GridView control handles the select operation. This event is often used to cancel the selecting operation.

The following example demonstrates how to use the AutoGenerateSelectButton property to enable the automatic selection feature of the GridView control.


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
  {
    // Get the currently selected row using the SelectedRow property.
    GridViewRow row = CustomersGridView.SelectedRow;

    // Display the first name from the selected row.
    // In this example, the third column (index 2) contains
    // the first name.
    MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
  }

  void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
  {
    // Get the currently selected row. Because the SelectedIndexChanging event
    // occurs before the select operation in the GridView control, the
    // SelectedRow property cannot be used. Instead, use the Rows collection
    // and the NewSelectedIndex property of the e argument passed to this 
    // event handler.
    GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];

    // You can cancel the select operation by using the Cancel
    // property. For this example, if the user selects a customer with 
    // the ID "ANATR", the select operation is canceled and an error message
    // is displayed.
    if (row.Cells[1].Text == "ANATR")
    {
      e.Cancel = true;
      MessageLabel.Text = "You cannot select " + row.Cells[2].Text + "."; 
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView Select Example</title>
</head>
<body>
    <form id="form1" runat="server">

     <h3>GridView Select Example</h3>

     <asp:gridview id="CustomersGridView" 
       datasourceid="CustomersSource" 
       autogeneratecolumns="False"
       autogenerateselectbutton="True"
       selectedindex="1"
       onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
       onselectedindexchanging="CustomersGridView_SelectedIndexChanging"   
       runat="server" DataKeyNames="CustomerID">

         <Columns>
             <asp:BoundField DataField="CustomerID" 
                 HeaderText="CustomerID" 
                 InsertVisible="False" ReadOnly="True" 
                 SortExpression="CustomerID" />
             <asp:BoundField DataField="FirstName" 
                 HeaderText="FirstName" 
                 SortExpression="FirstName" />
             <asp:BoundField DataField="MiddleName" 
                 HeaderText="MiddleName" 
                 SortExpression="MiddleName" />
             <asp:BoundField DataField="LastName" 
                 HeaderText="LastName" 
                 SortExpression="LastName" />
             <asp:BoundField DataField="Phone" 
                 HeaderText="Phone" 
                 SortExpression="Phone" />
         </Columns>

       <selectedrowstyle backcolor="LightCyan"
         forecolor="DarkBlue"
         font-bold="true"/>  

     </asp:gridview>

      <br/>

      <asp:label id="MessageLabel"
        forecolor="Red"
        runat="server"/>

      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="SELECT CustomerID, FirstName, MiddleName, LastName, Phone FROM SalesLT.Customer"
        connectionstring="<%$ ConnectionStrings:AdventureWorksLTConnectionString %>" 
        runat="server"/>

    </form>
  </body>
</html>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft