DetailsView.AutoGenerateInsertButton 屬性

定義

取得或設定值,指出可插入新資料錄的內建控制項是否顯示於 DetailsView 控制項中。

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

屬性值

true 表示顯示可插入新資料錄的內建控制項,否則為 false。 預設為 false

範例

下列程式碼範例示範如何使用 AutoGenerateInsertButton 屬性來顯示內建控制項,以在 控制項中 DetailsView 插入新記錄。


<%@ 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 AutoGenerateInsertButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateInsertButton Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogenerateinsertbutton="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 AutoGenerateInsertButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateInsertButton Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogenerateinsertbutton="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.InsertCommand 屬性必須使用插入查詢語句來設定。

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

按一下時,[新增] 按鈕也會取代為 [插入] 按鈕和 [取消] 按鈕。 按一下 [插入] 按鈕會在資料來源中插入新記錄,並將控制項傳回屬性所 DefaultMode 指定的模式。 按一下 [取消] 按鈕會放棄插入作業,並將控制項傳回預設模式。

注意

若要以程式設計方式將資料列放入插入模式,請使用 ChangeMode 方法。

您可以使用 屬性來控制處於插入模式 InsertRowStyle 的記錄外觀。 一般設定通常包括自訂背景色彩、前景色彩和字型屬性。

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

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

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

適用於

另請參閱