CommandField 類別

定義

表示特殊欄位,該欄位會顯示在資料繫結控制項中執行選取、編輯、插入或刪除作業的命令按鈕。

public ref class CommandField : System::Web::UI::WebControls::ButtonFieldBase
public class CommandField : System.Web.UI.WebControls.ButtonFieldBase
type CommandField = class
    inherit ButtonFieldBase
Public Class CommandField
Inherits ButtonFieldBase
繼承

範例

下列程式碼範例示範如何使用 CommandField 欄位來顯示控制項,以編輯控制項中的 GridView 記錄。


<%@ 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:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        datakeynames="CustomerID"  
        runat="server">
        
        <columns>
          <asp:commandfield showeditbutton="true"
            headertext="Edit Controls"/>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID" />
          <asp:boundfield datafield="CompanyName"
            headertext="Company Name"/>
          <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"/>
        </columns>
        
      </asp:gridview>
            
      <!-- 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]"
        updatecommand="Update Customers Set CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)"
        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:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        datakeynames="CustomerID"  
        runat="server">
        
        <columns>
          <asp:commandfield showeditbutton="true"
            headertext="Edit Controls"/>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID" />
          <asp:boundfield datafield="CompanyName"
            headertext="Company Name"/>
          <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"/>
        </columns>
        
      </asp:gridview>
            
      <!-- 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]"
        updatecommand="Update Customers Set CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

下列程式碼範例示範如何使用 CancelTextEditTextUpdateText 屬性來自訂編輯控制項的文字。


<%@ 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:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        datakeynames="CustomerID"  
        runat="server">
        
        <columns>
          <asp:commandfield showeditbutton="true"
            edittext="Edit Customer"
            canceltext="Discard"
            updatetext="Revise"
            headertext="Edit Controls"/>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID" />
          <asp:boundfield datafield="CompanyName"
            headertext="Company Name"/>
          <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"/>
        </columns>
        
      </asp:gridview>
            
      <!-- 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]"
        updatecommand="Update Customers Set CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)"
        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:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        datakeynames="CustomerID"  
        runat="server">
        
        <columns>
          <asp:commandfield showeditbutton="true"
            edittext="Edit Customer"
            canceltext="Discard"
            updatetext="Revise"
            headertext="Edit Controls"/>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID" />
          <asp:boundfield datafield="CompanyName"
            headertext="Company Name"/>
          <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"/>
        </columns>
        
      </asp:gridview>
            
      <!-- 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]"
        updatecommand="Update Customers Set CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

備註

類別 CommandField 是資料繫結控制項 (的特殊欄位,例如 GridViewDetailsView) ,用來顯示執行刪除、編輯、插入或選取作業的命令按鈕。 執行這些作業的命令按鈕可以使用下表所示的屬性來顯示或隱藏。

屬性 描述
ShowDeleteButton 顯示或隱藏資料繫結控制項中每個記錄欄位中的 [刪除] 按鈕 CommandField 。 [刪除] 按鈕可讓使用者從資料來源中刪除記錄。
ShowEditButton 顯示或隱藏資料繫結控制項中每個記錄之 CommandField 欄位中的 [編輯] 按鈕。 [編輯] 按鈕可讓使用者從資料來源編輯記錄。 當使用者按一下特定記錄的 [編輯] 按鈕時,該 [編輯] 按鈕會取代為 [更新] 按鈕和 [取消] 按鈕。 所有其他命令按鈕也會隱藏。
ShowInsertButton 顯示或隱藏欄位中的 CommandField [新增] 按鈕。 [新增] 按鈕可讓使用者在資料來源中插入新記錄。 當使用者按一下 [新增] 按鈕時,它會取代為 [插入] 按鈕和 [取消] 按鈕。 所有其他命令按鈕也會隱藏。 注意: 此屬性只適用于支援插入作業的資料繫結控制項,例如 DetailsView 控制項。
ShowSelectButton 顯示或隱藏資料繫結控制項中每個記錄欄位中 CommandField 的 [選取] 按鈕。 [選取] 按鈕可讓使用者選取資料繫結控制項中的記錄。

此外,設定 屬性可以顯示或隱藏 ShowCancelButton 記錄處於編輯或插入模式時顯示的 [取消] 按鈕。

注意

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

欄位 CommandField 會根據所使用的資料繫結控制項,以不同的方式顯示。 例如,控制項會將 GridView 欄位顯示為 CommandField 資料行,而控制項則會 DetailsView 將其顯示為數據列。

若要指定要顯示的按鈕類型,請使用 ButtonType 屬性。 ButtonType當 屬性設定為 ButtonType.ButtonButtonType.Link 時,您可以藉由設定下表中顯示的屬性,來指定要顯示按鈕的文字。

屬性 描述
CancelText [取消] 按鈕的標題。
DeleteText [刪除] 按鈕的標題。
EditText [編輯] 按鈕的標題。
InsertText [插入] 按鈕的標題。 注意: 此屬性只適用于支援插入作業的資料繫結控制項,例如 DetailsView 控制項。
NewText [新增] 按鈕的標題。 注意: 此屬性只適用于支援插入作業的資料繫結控制項,例如 DetailsView 控制項。
SelectText [選取] 按鈕的標題。
UpdateText [更新] 按鈕的標題。

您可以將 屬性設定 ButtonTypeButtonType.Image ,然後設定下表中顯示的屬性,而不是顯示命令按鈕或連結按鈕。

屬性 描述
CancelImageUrl 要針對 [取消] 按鈕顯示的影像
DeleteImageUrl 要針對 [刪除] 按鈕顯示的影像。
EditImageUrl 要針對 [編輯] 按鈕顯示的影像。
InsertText 要針對 [插入] 按鈕顯示的影像。 注意: 此屬性只適用于支援插入作業的資料繫結控制項,例如 DetailsView 控制項。
NewImageUrl 要針對 [新增] 按鈕顯示的影像。 注意: 此屬性只適用于支援插入作業的資料繫結控制項,例如 DetailsView 控制項。
SelectImageUrl 要針對 [選取] 按鈕顯示的影像。
UpdateImageUrl 要針對 [更新] 按鈕顯示的影像。

根據預設,當使用者按一下欄位中的 CommandField 按鈕時,會在頁面上的所有驗證控制項上執行驗證。 若要防止按一下按鈕時發生驗證,請將 CausesValidation 屬性設定為 false

您可以將 屬性設定 Visiblefalse ,以隱藏 CommandField 資料繫結控制項中的欄位。

欄位 CommandField 可讓您自訂其頁首和頁尾區段。 若要在頁首或頁尾區段中顯示標題,請分別設定 HeaderTextFooterText 屬性。 您可以藉由設定 HeaderImageUrl 屬性來顯示影像,而不是在標頭區段中顯示文字。 若要隱藏 物件中的 CommandField 標頭區段,請將 ShowHeader 屬性設定為 false

注意

某些資料繫結控制項 (例如 GridView 控制項) 只能顯示或隱藏控制項的整個標頭區段。 這些資料繫結控制項不支援 ShowHeader 個別按鈕欄位的屬性。 若要顯示或隱藏資料繫結控制項的整個標頭區段,如果可用) ,請使用控制項的 ShowHeader 屬性 (。

您也可以藉由為欄位的不同部分設定樣式屬性,來自訂物件的外觀 CommandField (字型色彩、背景色彩等) 。 下表列出不同的樣式屬性。

Style 屬性 描述
ControlStyle 物件的子 Web 服務器控制項 CommandField 樣式設定。
FooterStyle 物件的頁尾區段樣式 CommandField 設定。
HeaderStyle 物件的標頭區段 CommandField 樣式設定。
ItemStyle 物件中資料項目的 CommandField 樣式設定。

建構函式

CommandField()

初始化 CommandField 類別的新執行個體。

屬性

AccessibleHeaderText

取得或設定在部分控制項中呈現為 AbbreviatedText 屬性值的文字。

(繼承來源 DataControlField)
ButtonType

取得或設定要在按鈕欄位中顯示的按鈕類型。

(繼承來源 ButtonFieldBase)
CancelImageUrl

取得或設定要為 CommandField 欄位中 [取消] 按鈕顯示之影像的 URL。

CancelText

取得或設定顯示在 CommandField 欄位中之 [取消] 按鈕的標題。

CausesValidation

取得或設定值,指出是否要在使用者按一下 CommandField 欄位中的按鈕時執行驗證。

Control

取得與 DataControlField 物件關聯之資料控制項的參考。

(繼承來源 DataControlField)
ControlStyle

取得 DataControlField 物件內含之任何 Web 伺服器控制項的樣式。

(繼承來源 DataControlField)
DeleteImageUrl

取得或設定要為 CommandField 欄位中 [刪除] 按鈕顯示之影像的 URL。

DeleteText

取得或設定 CommandField 欄位中 [刪除] 按鈕的標題。

DesignMode

取得值,指示目前是否在設計階段環境中檢視資料控制項欄位。

(繼承來源 DataControlField)
EditImageUrl

取得或設定要為 CommandField 欄位中 [編輯] 按鈕顯示之影像的 URL。

EditText

取得或設定 CommandField 欄位中 [編輯] 按鈕的標題。

FooterStyle

取得或設定資料控制項欄位的頁尾樣式。

(繼承來源 DataControlField)
FooterText

取得或設定顯示在資料控制項欄位之頁尾項目中的文字。

(繼承來源 DataControlField)
HeaderImageUrl

取得或設定顯示在資料控制項欄位的標頭項目中之影像的 URL。

(繼承來源 DataControlField)
HeaderStyle

取得或設定資料控制項欄位的標頭樣式。

(繼承來源 DataControlField)
HeaderText

取得或設定顯示在資料控制項欄位之標頭項目中的文字。

(繼承來源 DataControlField)
InsertImageUrl

取得或設定要為 CommandField 欄位中 [插入] 按鈕顯示之影像的 URL。

InsertText

取得或設定 CommandField 欄位中 [插入] 按鈕的標題。

InsertVisible

取得值,指示 DataControlField 物件在其父資料繫結控制項處於插入模式時是否可見。

(繼承來源 DataControlField)
IsTrackingViewState

取得值,指出 DataControlField 物件是否正在將變更儲存到它的檢視狀態。

(繼承來源 DataControlField)
ItemStyle

取得由資料控制項欄位顯示之任何文字基礎內容的樣式。

(繼承來源 DataControlField)
NewImageUrl

取得或設定要為 CommandField 欄位中 [新增] 按鈕顯示之影像的 URL。

NewText

取得或設定 CommandField 欄位中 [新增] 按鈕的標題。

SelectImageUrl

取得或設定要為 CommandField 欄位中 [選取] 按鈕顯示之影像的 URL。

SelectText

取得或設定 CommandField 欄位中 [選取] 按鈕的標題。

ShowCancelButton

取得或設定值,指出是否要在 CommandField 欄位中顯示 [取消] 按鈕。

ShowDeleteButton

取得或設定值,指出是否要在 CommandField 欄位中顯示 [刪除] 按鈕。

ShowEditButton

取得或設定值,指出是否要在 CommandField 欄位中顯示 [編輯] 按鈕。

ShowHeader

取得或設定值,表示是否在 ButtonFieldBase 物件中顯示標頭區段。

(繼承來源 ButtonFieldBase)
ShowInsertButton

取得或設定值,指出是否要在 CommandField 欄位中顯示 [新增] 按鈕。

ShowSelectButton

取得或設定值,指出是否要在 CommandField 欄位中顯示 [選取] 按鈕。

SortExpression

取得或設定資料來源控制項用於排序資料的排序運算式。

(繼承來源 DataControlField)
UpdateImageUrl

取得或設定要為 CommandField 欄位中 [更新] 按鈕顯示之影像的 URL。

UpdateText

取得或設定 CommandField 欄位中 [更新] 按鈕的標題。

ValidateRequestMode

取得或設定值,這個值會指定控制項是否驗證用戶端輸入。

(繼承來源 DataControlField)
ValidationGroup

取得或設定在按一下 ButtonFieldBase 物件中的按鈕時要驗證的驗證控制項群組名稱。

(繼承來源 ButtonFieldBase)
ViewState

取得狀態資訊的字典,允許您在相同頁面的多個要求之間,儲存和還原 DataControlField 物件的檢視狀態。

(繼承來源 DataControlField)
Visible

取得或設定值,指示是否呈現資料控制項欄位。

(繼承來源 DataControlField)

方法

CloneField()

建立目前 DataControlField 衍生物件的複本。

(繼承來源 DataControlField)
CopyProperties(DataControlField)

將目前 CommandField 物件的屬性複製到指定的 DataControlField 物件。

CreateField()

建立空的 CommandField 物件。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
ExtractValuesFromCell(IOrderedDictionary, DataControlFieldCell, DataControlRowState, Boolean)

從目前資料表儲存格中擷取資料控制項欄位的值,並將值加入至指定的 IDictionary 集合。

(繼承來源 DataControlField)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
Initialize(Boolean, Control)

執行資料控制項欄位的基本執行個體初始化。

(繼承來源 DataControlField)
InitializeCell(DataControlFieldCell, DataControlCellType, DataControlRowState, Int32)

初始化指定的 DataControlFieldCell 物件為指定的資料列狀態。

LoadViewState(Object)

將資料來源檢視還原成之前所儲存的檢視狀態。

(繼承來源 DataControlField)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnFieldChanged()

引發 FieldChanged 事件。

(繼承來源 DataControlField)
SaveViewState()

儲存自頁面回傳至伺服器以來對 DataControlField 檢視狀態所做的變更。

(繼承來源 DataControlField)
ToString()

傳回字串,表示這個 DataControlField 物件。

(繼承來源 DataControlField)
TrackViewState()

會造成 DataControlField 物件追蹤其檢視狀態變更,以將這些變更儲存在控制項的 ViewState 屬性中,並持續存取相同頁面的其他要求。

(繼承來源 DataControlField)
ValidateSupportsCallback()

判斷 CommandField 物件中所包含的控制項是否支援回呼 (Callback)。

明確介面實作

IDataSourceViewSchemaAccessor.DataSourceViewSchema

取得或設定與此 DataControlField 物件相關聯的結構描述。

(繼承來源 DataControlField)
IStateManager.IsTrackingViewState

取得值,指出 DataControlField 物件是否正在將變更儲存到它的檢視狀態。

(繼承來源 DataControlField)
IStateManager.LoadViewState(Object)

將資料控制項欄位還原成先前儲存的檢視狀態。

(繼承來源 DataControlField)
IStateManager.SaveViewState()

儲存自頁面回傳至伺服器以來對 DataControlField 檢視狀態所做的變更。

(繼承來源 DataControlField)
IStateManager.TrackViewState()

會造成 DataControlField 物件追蹤其檢視狀態變更,以將這些變更儲存在控制項的 ViewState 屬性中,並持續存取相同頁面的其他要求。

(繼承來源 DataControlField)

適用於

另請參閱