DetailsView.AutoGenerateEditButton 屬性

定義

取得或設定值,指出可編輯目前資料錄的內建控制項是否顯示於 DetailsView 控制項中。

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

屬性值

Boolean

true 表示顯示可編輯目前資料錄的內建控制項,否則為 false。 預設為 false

範例

下列程式碼範例示範如何使用 AutoGenerateEditButton 屬性來顯示內建控制項來編輯目前記錄。


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView AutoGenerateEditButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateEditButton Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogenerateeditbutton="true"  
          autogeneraterows="true"
          allowpaging="true"  
          runat="server">
               
          <headerstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <!-- 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="DetailsViewSource" runat="server" 
          ConnectionString=
            "<%$ ConnectionStrings:NorthWindConnectionString%>"
          InsertCommand="INSERT INTO [Customers]([CustomerID], 
            [CompanyName], [Address], [City], [PostalCode], [Country]) 
            VALUES (@CustomerID, @CompanyName, @Address, @City, 
            @PostalCode, @Country)"
          SelectCommand="Select [CustomerID], [CompanyName], 
            [Address], [City], [PostalCode], [Country] 
            From [Customers]">
        </asp:SqlDataSource>
    </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">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView AutoGenerateEditButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateEditButton Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogenerateeditbutton="true"  
          autogeneraterows="true"
          allowpaging="true"  
          runat="server">
               
          <headerstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <!-- 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="DetailsViewSource" runat="server" 
          ConnectionString=
            "<%$ ConnectionStrings:NorthWindConnectionString%>"
          InsertCommand="INSERT INTO [Customers]([CustomerID], 
            [CompanyName], [Address], [City], [PostalCode], [Country]) 
            VALUES (@CustomerID, @CompanyName, @Address, @City, 
            @PostalCode, @Country)"
          SelectCommand="Select [CustomerID], [CompanyName], 
            [Address], [City], [PostalCode], [Country] 
            From [Customers]">
        </asp:SqlDataSource>
    </form>
  </body>
</html>

備註

當支援更新的資料來源控制項系結至 DetailsView 控制項時, DetailsView 控制項可以利用資料來源控制項的功能並提供自動更新功能。

注意

若要讓資料來源控制項更新資料,其 SqlDataSource.UpdateCommand 屬性必須使用更新查詢語句來設定。

AutoGenerateEditButton當 屬性設定為 true 時, CommandField 控制項中 DetailsView 會自動顯示具有 [編輯] 按鈕的資料欄欄位。 按一下 [編輯] 按鈕會將該 DetailsView 控制項置於編輯模式。 在編輯模式中時,控制項中不是唯讀的每個系結欄位都會顯示欄位資料類型的適當輸入控制項,例如 TextBox 控制項。 這可讓使用者修改域的值。

按一下時,[編輯] 按鈕也會取代為 [更新] 按鈕和 [取消] 按鈕。 按一下 [更新] 按鈕會以任何值變更來更新資料來源中的記錄,並將控制項傳回 屬性所 DefaultMode 指定的模式。 按一下 [取消] 按鈕會放棄任何值變更,並將控制項傳回預設模式。

注意

若要以程式設計方式將控制項放在 DetailsView 編輯模式中,請使用 ChangeMode 方法。

AutoGenerateRows當 屬性也設定為 true 時, DetailsView 控制項會自動確保屬性中指定的 DataKeyNames 欄位或欄位是唯讀的。

注意

除非您實作自己的更新功能,否則您必須設定 DataKeyNames 自動更新功能的 屬性才能運作。

當控制項處於編輯模式 EditRowStyleDetailsView ,您可以使用 屬性來控制資料列的外觀。 一般設定通常包括自訂背景色彩、前景色彩和字型屬性。

控制項 DetailsView 提供數個事件,您可以在更新記錄時用來執行自訂動作。 下表列出可用的事件。

事件 描述
ItemUpdated 發生于按一下 [更新] 按鈕,但在控制項更新記錄之後 DetailsView 。 此事件通常用來檢查更新作業的結果。
ItemUpdating 發生于按一下 [更新] 按鈕,但在控制項更新記錄之前 DetailsView 。 此事件通常用來取消更新作業。
ModeChanged 發生于 DetailsView 控制項變更模式之後。
ModeChanging 發生于 DetailsView 控制項變更模式之前。 此事件通常用來取消模式變更。

的值 AutoGenerateEditButton 會儲存在檢視狀態中。

適用於

另請參閱