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

屬性值

若要在 CommandField 中顯示 [新增] 按鈕,則為 true,否則為 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 ,並可讓使用者在資料來源中新增記錄。

注意

此屬性只適用于支援插入作業的資料繫結控制項,例如 DetailsView 控制項。

當使用者按一下 [新增] 按鈕時,會針對資料繫結控制項中顯示的每個欄位顯示輸入控制項,讓使用者輸入新記錄的值。 [新增] 按鈕會取代為 [插入] 按鈕和 [取消] 按鈕,且欄位中所有其他命令按鈕 CommandField 都會隱藏。 按一下 [插入] 按鈕會將記錄新增至資料來源,而按一下 [取消] 按鈕會取消作業。

注意

當資料繫結控制項與資料來源控制項搭配使用時, (例如 SqlDataSource 控制項) ,資料繫結控制項可以利用資料來源控制項的功能並提供自動插入功能。 對於其他資料來源,您必須提供常式,以在資料繫結控制項的適當事件期間執行插入作業。

ButtonType當欄位的 CommandField 屬性設定為 ButtonType.ButtonButtonType.Link 時,請使用 NewText 屬性來指定要顯示 [新增] 按鈕的文字。 或者,您可以先將 ButtonType 屬性設定為 ButtonType.Image ,然後設定 NewImageUrl 屬性來顯示影像。

適用於

另請參閱