GridView.AutoGenerateSelectButton 属性

定义

获取或设置一个值,该值指示每个数据行都带有“选择”按钮的 CommandField 字段列是否自动添加到 GridView 控件。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.

public:
 virtual property bool AutoGenerateSelectButton { bool get(); void set(bool value); };
public virtual bool AutoGenerateSelectButton { get; set; }
member this.AutoGenerateSelectButton : bool with get, set
Public Overridable Property AutoGenerateSelectButton As Boolean

属性值

true 表示自动添加在每个数据行都带有“选择”按钮的 CommandField 字段列;否则为 falsetrue to automatically add a CommandField field column with a Select button for each data row; otherwise, false. 默认值为 falseThe default is false.

示例

下面的示例演示如何使用AutoGenerateSelectButton属性启用GridView控件的自动选择功能。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>


<%@ Page language="VB" %>

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

  Sub CustomersGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        
    ' Get the currently selected row using the SelectedRow property.
    Dim row As GridViewRow = 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 & "."
  End Sub

  Sub CustomersGridView_SelectedIndexChanging(ByVal sender As Object, ByVal e As GridViewSelectEventArgs)
        
    ' 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.
    Dim row As GridViewRow = 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" Then
        e.Cancel = True
        MessageLabel.Text = "You cannot select " + row.Cells(2).Text & "."
    End If
    
  End Sub

</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>

注解

当属性设置为trueCommandField ,GridView每个数据行的 "选择" 按钮都将自动添加到控件中。 AutoGenerateSelectButtonWhen 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. 单击某一行的 "选择" 按钮将在控件中选择该行, 这会SelectedIndex将该属性设置为行的索引。Clicking the Select button for a row selects that row in the control, which sets the SelectedIndex property to the index of the row. 若要检索GridViewRow表示所选行的对象, 请SelectedRow使用属性。To retrieve the GridViewRow object that represents the selected row, use the SelectedRow property. 还可以通过使用SelectedValue属性来获取所选记录的主键值。You can also get the primary key value for the selected record by using the SelectedValue property. 属性包含DataKeyNames属性中指定的键字段的值。 SelectedValueThe SelectedValue property contains the values of the key fields specified in the DataKeyNames property.

备注

可以通过设置SelectedIndex属性以编程方式选择行。You can programmatically select a row by setting the SelectedIndex property. 若要取消选择某一行, 请将SelectedIndex属性设置为-1。To cancel the selection of a row, set the SelectedIndex property to -1.

您可以使用SelectedRowStyle属性控制所选行的外观。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.

GridView控件提供若干事件, 可用于在选择行时执行自定义操作。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 发生在单击某一行的“选择”按钮,GridView 控件对相应的选择操作进行处理之后。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 发生在单击某一行的“选择”按钮以后,GridView 控件对相应的选择操作进行处理之前。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.

适用于

另请参阅