語言

CommandField.ShowInsertButton 屬性

定義

取得或設定一個值,表示欄位中是否顯示 CommandField 新按鈕。

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

屬性值

true在 中顯示新按鈕CommandField;否則,。 false 預設值為 false。

範例

以下程式碼範例示範如何使用該 ShowInsertButton 屬性在控制項中顯示新按鈕 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>CommandField Example</title>
</head>
<body>
    <form id="form1" runat="server">
      
      <h3>CommandField Example</h3>

      <asp:detailsview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneraterows="false"
        datakeynames="CustomerID"  
        allowpaging="true" 
        runat="server">
        
        <fields>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID" />
          <asp:boundfield datafield="CompanyName"
            headertext="CompanyName"/>
          <asp:boundfield datafield="Address"
            headertext="Address"/>
          <asp:boundfield datafield="City"
            headertext="City"/>
          <asp:boundfield datafield="PostalCode"
            headertext="ZIP Code"/>
          <asp:boundfield datafield="Country"
            headertext="Country"/>
          <asp:commandfield showinsertbutton="true"
            showheader="true"
            headertext="Add Customer"/>
        </fields>
        
      </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="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        insertcommand="Insert Into [Customers]([CustomerID], [CompanyName], [City], [PostalCode], [Country]) Values (@CustomerID, @CompanyName, @City, @PostalCode, @Country)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </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>CommandField Example</title>
</head>
<body>
    <form id="form1" runat="server">
      
      <h3>CommandField Example</h3>

      <asp:detailsview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneraterows="false"
        datakeynames="CustomerID"  
        allowpaging="true" 
        runat="server">
        
        <fields>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID" />
          <asp:boundfield datafield="CompanyName"
            headertext="CompanyName"/>
          <asp:boundfield datafield="Address"
            headertext="Address"/>
          <asp:boundfield datafield="City"
            headertext="City"/>
          <asp:boundfield datafield="PostalCode"
            headertext="ZIP Code"/>
          <asp:boundfield datafield="Country"
            headertext="Country"/>
          <asp:commandfield showinsertbutton="true"
            showheader="true"
            headertext="Add Customer"/>
        </fields>
        
      </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="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        insertcommand="Insert Into [Customers]([CustomerID], [CompanyName], [City], [PostalCode], [Country]) Values (@CustomerID, @CompanyName, @City, @PostalCode, @Country)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

備註

使用該 ShowInsertButton 屬性來指定欄位中是否顯示 CommandField 新按鈕。 新增按鈕在欄位中僅顯示一次 CommandField ,允許使用者在資料來源中新增紀錄。

Note

此特性僅適用於支援插入操作的資料綁定控制項,例如 控制 DetailsView 項。

當使用者點擊新按鈕時,資料綁定控制項中每個欄位的輸入控制會顯示,讓使用者輸入新紀錄的值。 新按鈕入按鈕和取消按鈕取代,欄位中 CommandField 其他所有指令按鈕都被隱藏。 點擊插入按鈕會將記錄加入資料來源,而點擊取消按鈕則會取消操作。

Note

當資料綁定控制項與資料來源控制(例如 SqlDataSource 控制項)結合使用時,資料綁定控制項可以利用資料來源控制的功能,提供自動插入功能。 對於其他資料來源,你必須提供例程,以便在資料綁定控制的適當事件中執行插入操作。

當 ButtonType 欄位 CommandField 的屬性設定為 ButtonType.Button 或 ButtonType.Link時,請使用該 NewText 屬性指定新按鈕要顯示的文字。 或者,你也可以先將屬性設定 ButtonType 為 , ButtonType.Image 再設定該 NewImageUrl 屬性來顯示圖片。

適用於

另請參閱