GridView.AutoGenerateEditButton 属性

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

 

C#
public virtual bool AutoGenerateEditButton { get; set; }

 

 

属性值

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

当支持更新的数据源控件绑定到 GridView 控件时,GridView 控件可利用该数据源控件的功能并提供自动更新功能。

Note注意

为了使数据源控件可更新数据,必须对它进行相应配置。若要配置数据源控件以更新记录,请参见特定数据源控件的文档。

AutoGenerateEditButton 属性设置为 true 时,在每个数据行都带有“编辑”按钮的列(由 CommandField 对象表示)被自动添加到 GridView 控件。单击某行的“编辑”按钮可将该行置于编辑模式。当行处于编辑模式时,非只读行中的每个列字段显示对应于该字段的数据类型的输入控件,如 TextBox 控件。这允许用户修改该字段的值。

当单击“编辑”按钮时,它还会被“更新”按钮和“取消”按钮替换。单击“更新”按钮以任何值的更改更新数据源中的行,并将该行返回为显示模式。单击“取消”按钮放弃任何值更改,并将该行返回为显示模式。

Note注意

通过使用行的索引来设置 EditIndex 属性,可以编程方式将行置于编辑模式。若要以编程方式退出编辑模式,请将 EditIndex 属性设置为 -1。

在使用内置更新功能时,必须用一个逗号分隔的字段名列表来设置 DataKeyNames 属性,以标识数据源的主键字段;否则,内置更新功能将无法更新正确的记录。在使用自动生成的字段列时(通过将 AutoGenerateColumns 属性设置为 true),GridView 控件自动确保与 DataKeyNames 属性中指定的字段相对应的自动生成字段列是只读的。

通过使用 EditRowStyle 属性,可控制处于编辑模式的行的外观。常用设置通常包括自定义背景色、前景色和字体属性。

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

事件

说明

RowCancelingEdit

在单击某一行的“取消”按钮时,但在 GridView 控件退出编辑模式之前发生。此事件通常用于停止取消操作。

RowEditing

发生在单击某一行的“编辑”按钮以后,GridView 控件进入编辑模式之前。此事件通常用于取消编辑操作。

RowUpdated

发生在单击某一行的“更新”按钮,并且 GridView 控件对该行进行更新之后。此事件通常用于检查更新操作的结果。

RowUpdating

发生在单击某一行的“更新”按钮以后,GridView 控件对该行进行更新之前。此事件通常用于取消更新操作。

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

 

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

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

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView"
        datasourceid="CustomersSqlDataSource"
        autogeneratecolumns="true"
        autogeneratedeletebutton="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID" 
        runat="server">
      </asp:gridview>
           
      <!-- 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="CustomersSqlDataSource" 
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
           
    </form>
  </body>
</html>

 

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