DetailsView.AutoGenerateDeleteButton 屬性

定義

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

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

屬性值

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

範例

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


<%@ 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 AutoGenerateDeleteButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateDeleteButton Example</h3>
                
        <asp:detailsview id="CustomersDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneratedeletebutton="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 AutoGenerateDeleteButton Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView AutoGenerateDeleteButton Example</h3>
                
        <asp:detailsview id="CustomersDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneratedeletebutton="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當控制項系結至繼承自 DataSourceViewCanDelete 屬性傳回 true 的物件時, DetailsView 控制項可以利用資料來源控制項的功能,並提供自動刪除功能。

注意

SqlDataSourceView若要刪除資料的物件, SqlDataSource.DeleteCommand 基礎 SqlDataSource 物件的 屬性必須使用 delete 查詢語句來設定。

AutoGenerateDeleteButton當 屬性設定為 true 時, CommandField 控制項中 DetailsView 會自動顯示具有 [刪除] 按鈕的資料欄欄位。 按一下 [刪除] 按鈕會永久移除資料來源中的該記錄。

注意

您也必須設定 DataKeyNames 自動刪除功能的 屬性才能運作。

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

事件 描述
ItemDeleted 發生于按一下 [刪除] 按鈕時,但在控制項從資料來源中刪除記錄之後 DetailsView 。 此事件通常用來檢查刪除作業的結果。
ItemDeleting 發生于按一下 [刪除] 按鈕,但在控制項從資料來源中刪除記錄之前 DetailsView 。 此事件通常用來取消刪除作業。

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

適用於

另請參閱