GridView.AutoGenerateSelectButton 属性

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

 

public virtual bool AutoGenerateSelectButton { get; set; }

 

 

属性值

true 表示自动添加在每个数据行都带有“选择”按钮的 CommandField 字段列;否则为 false。默认为 false

AutoGenerateSelectButton 属性设置为 true 时,在每个数据行都带有“编辑”按钮的列(由 CommandField 对象表示)被自动添加到 GridView 控件。单击某行的“选择”按钮在控件中选中该行,这会将 SelectedIndex 属性设置为该行的索引。若要检索表示选中行的 GridViewRow 对象,请使用 SelectedRow 属性。通过使用 SelectedValue 属性,还可以获得选中记录的主键值。SelectedValue 属性包含在 DataKeyNames 属性中指定的键字段的值。

Note注意

通过设置 SelectedIndex 属性,可以编程方式选择行。若要取消行的选择,请将 SelectedIndex 属性设置为 -1。

可以通过使用 SelectedRowStyle 属性控制选中行的外观。常用设置通常包括自定义背景色、前景色和字体属性。

GridView 控件提供多个可以用于在选择行时执行自定义操作的事件。下表列出了可用的事件。

事件

说明

SelectedIndexChanged

发生在单击某一行的“选择”按钮,GridView 控件对相应的选择操作进行处理之后。此事件通常用于在该控件中选定某行之后执行某项任务。

SelectedIndexChanging

发生在单击某一行的“选择”按钮以后,GridView 控件对相应的选择操作进行处理之前。此事件通常用于取消选择操作。

下面的代码示例演示如何使用 AutoGenerateSelectButton 属性来启用 GridView 控件的自动选择功能。

 

下面的代码示例演示如何使用 AutoGenerateSelectButton 属性来启用 GridView 控件的自动选择功能。

 

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

<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 company name from the selected row.
    // In this example, the third column (index 2) contains
    // the company name.
    Message.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;
      Message.Text = "You cannot select " + row.Cells[2].Text + ".";
       
    }
   
  }

</script>

<html>
  <body>
    <form runat="server">
       
     <h3>GridView Select Example</h3>

     <asp:gridview id="CustomersGridView"
       datasourceid="CustomersSource"
       autogeneratecolumns="true"
       autogenerateselectbutton="true"
       allowpaging="true"
       selectedindex="0"
       onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
       onselectedindexchanging="CustomersGridView_SelectedIndexChanging"  
       runat="server">
               
       <selectedrowstyle backcolor="LightCyan"
         forecolor="DarkBlue"
         font-bold="true"/> 
               
     </asp:gridview>
           
      <br/>
           
      <asp:label id="Message"
        forecolor="Red"
        runat="server"/>
           
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind 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], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server"/>
           
    </form>
  </body>
</html>

 

posted @ 2009-04-17 10:15  minmin8110  阅读(561)  评论(0)    收藏  举报