GridView.AutoGenerateSelectButton 속성

정의

각 데이터 행의 선택 단추가 있는 CommandField 필드 열을 GridView 컨트롤에 자동으로 추가할지 여부를 나타내는 값을 가져오거나 설정합니다.

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

속성 값

Boolean

각 데이터 행의 선택 단추가 있는 CommandField 필드 열을 자동으로 추가하려면 true이고, 그렇지 않으면 false입니다. 기본값은 false입니다.

예제

다음 예제에서는 컨트롤의 AutoGenerateSelectButton 자동 선택 기능을 사용 하도록 설정 하는 속성을 사용 하는 방법을 보여 줍니다 GridView .


<%@ 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>

설명

AutoGenerateSelectButton 속성을 설정true하면 각 데이터 행에 대해 선택 단추가 있는 열(개체로 CommandField 표시됨)이 컨트롤에 GridView 자동으로 추가됩니다. 행에 대한 선택 단추를 클릭하면 컨트롤에서 해당 행이 선택되어 속성이 SelectedIndex 행의 인덱스로 설정됩니다. 선택한 행을 GridViewRow 나타내는 개체를 검색하려면 속성을 사용합니다 SelectedRow . 속성을 사용하여 SelectedValue 선택한 레코드의 기본 키 값을 가져올 수도 있습니다. 속성에는 SelectedValue 속성에 지정된 키 필드의 값이 DataKeyNames 포함됩니다.

참고

속성을 설정하여 프로그래밍 방식으로 행을 SelectedIndex 선택할 수 있습니다. 행 선택을 취소하려면 속성을 -1로 설정합니다 SelectedIndex .

속성을 사용하여 선택한 행의 모양을 제어할 SelectedRowStyle 수 있습니다. 일반적인 설정에는 일반적으로 사용자 지정 배경색, 전경색 및 글꼴 속성이 포함됩니다.

컨트롤은 GridView 행을 선택할 때 사용자 지정 작업을 수행하는 데 사용할 수 있는 여러 이벤트를 제공합니다. 다음 표에서는 사용 가능한 이벤트를 나열합니다.

이벤트 설명
SelectedIndexChanged 행의 선택 단추를 클릭하면 GridView 컨트롤이 선택 작업을 처리한 후에 이 이벤트가 발생합니다. 이 이벤트는 컨트롤에서 행을 선택한 후 작업을 수행하는 데 자주 사용됩니다.
SelectedIndexChanging 행의 선택 단추를 클릭하면 GridView 컨트롤이 선택 작업을 처리하기 전에 이 이벤트가 발생합니다. 이 이벤트는 종종 선택 작업을 취소하는 데 사용됩니다.

적용 대상

추가 정보