ButtonField 類別

定義

表示在資料繫結控制項中顯示為按鈕的欄位。

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

範例

下列程式碼範例示範如何使用 ButtonField 物件,在 控制項中 GridView 顯示命令按鈕的資料行。


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
  
    // If multiple ButtonField column fields are used, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Select")
    {
    
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);    
    
      // Get the last name of the selected author from the appropriate
      // cell in the GridView control.
      GridViewRow selectedRow = CustomersGridView.Rows[index];
      TableCell contactName = selectedRow.Cells[1];
      string contact = contactName.Text;  
    
      // Display the selected author.
      Message.Text = "You selected " + contact + ".";
      
    }
    
  }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ButtonField Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>ButtonField Example</h3>
      
      <asp:label id="Message"
        forecolor="Red"
        runat="server"
        AssociatedControlID="CustomersGridView"/>
                    
      <!-- Populate the Columns collection declaratively. -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        onrowcommand="CustomersGridView_RowCommand"
        runat="server">
                
        <columns>
                
          <asp:buttonfield buttontype="Button" 
            commandname="Select"
            headertext="Select Customer" 
            text="Select"/>
          <asp:boundfield datafield="CompanyName" 
            headertext="Company Name"/>
          <asp:boundfield datafield="ContactName" 
            headertext="Contact Name"/>
                
        </columns>
                
      </asp:gridview>
            
        <!-- This example uses Microsoft SQL Server and connects -->
        <!-- to the Northwind sample database.                   -->
        <asp:sqldatasource id="CustomersSqlDataSource"  
          selectcommand="Select [CustomerID], [CompanyName], [ContactName], [ContactTitle] From [Customers]"
          connectionstring="<%$ ConnectionStrings:NorthWindConnection%>"
          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">
<script runat="server">

    Sub CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
  
        ' If multiple ButtonField column fields are used, use the
        ' CommandName property to determine which button was clicked.
        If e.CommandName = "Select" Then
    
            ' Convert the row index stored in the CommandArgument
            ' property to an Integer.
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    
            ' Get the last name of the selected author from the appropriate
            ' cell in the GridView control.
            Dim selectedRow As GridViewRow = CustomersGridView.Rows(index)
            Dim contactCell As TableCell = selectedRow.Cells(1)
            Dim contact As String = contactCell.Text
    
            ' Display the selected author.
            Message.Text = "You selected " & contact & "."
      
        End If
    
    End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ButtonField Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>ButtonField Example</h3>
      
      <asp:label id="Message"
        forecolor="Red"
        runat="server"
        AssociatedControlID="CustomersGridView"/>
                    
      <!-- Populate the Columns collection declaratively. -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        onrowcommand="CustomersGridView_RowCommand"
        runat="server">
                
        <columns>
                
          <asp:buttonfield buttontype="Button" 
            commandname="Select"
            headertext="Select Customer" 
            text="Select"/>
          <asp:boundfield datafield="CompanyName" 
            headertext="Company Name"/>
          <asp:boundfield datafield="ContactName" 
            headertext="Contact Name"/>
                
        </columns>
                
      </asp:gridview>
            
        <!-- This example uses Microsoft SQL Server and connects -->
        <!-- to the Northwind sample database.                   -->
        <asp:sqldatasource id="CustomersSqlDataSource"  
          selectcommand="Select [CustomerID], [CompanyName], [ContactName], [ContactTitle] From [Customers]"
          connectionstring="<%$ ConnectionStrings:NorthWindConnection%>"
          runat="server">
        </asp:sqldatasource>
            
    </form>
  </body>
</html>

備註

資料 ButtonField 繫結控制項會使用 類別 (,例如 GridViewDetailsView) 來顯示每個顯示的記錄按鈕。 根據所使用的資料繫結控制項,物件 ButtonField 會以不同的方式顯示。 例如,控制項會將 GridView 物件顯示為 ButtonField 資料行,而 控制項則會 DetailsView 將其顯示為數據列。

按一下按鈕欄位中的按鈕會引發父資料繫結控制項的命令事件。 您可以藉由提供命令事件的事件處理常式,提供自訂常式,以在按一下命令按鈕時執行。

注意

控制項 GridViewRowCommand 引發 事件,而 DetailsView 控制項則會 ItemCommand 引發 事件。

若要判斷引發命令事件的記錄索引,請使用 CommandArgument 傳遞至資料繫結控制項之命令事件之事件引數的 屬性。 類別 ButtonField 會自動將適當的索引值填入 CommandArgument 屬性。

若要指定要顯示的按鈕類型,請使用 ButtonType 屬性。 顯示連結或命令按鈕時,請使用 Text 屬性來指定要顯示在按鈕中的標題。

注意

如果您設定 Text 屬性,則 ButtonField 中的所有按鈕都會共用相同的標題。

或者,您可以將 物件系結 ButtonField 至資料來源中的欄位。 這可讓您顯示 物件中 ButtonField 按鈕的不同標題。 指定欄位中的值會用於按鈕的文字標題。 設定 屬性, DataTextField 將 物件系結 ButtonField 至資料來源中的欄位。

顯示影像按鈕時,請使用 ImageUrl 屬性來指定要顯示物件中按鈕的 ButtonField 影像。

注意

物件中的所有 ButtonField 按鈕都會共用相同的影像。

您可以將 屬性設定 Visiblefalse ,以隱藏 ButtonField 資料繫結控制項中的 物件。

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

注意

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

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

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

建構函式

ButtonField()

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

屬性

AccessibleHeaderText

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

(繼承來源 DataControlField)
ButtonType

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

(繼承來源 ButtonFieldBase)
CausesValidation

取得或設定值,表示是否在按一下 ButtonFieldBase 物件中的按鈕時執行驗證。

(繼承來源 ButtonFieldBase)
CommandName

取得或設定字串,表示按一下 ButtonField 物件中的按鈕時要執行的動作。

Control

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

(繼承來源 DataControlField)
ControlStyle

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

(繼承來源 DataControlField)
DataTextField

取得或設定資料欄位的名稱,其值為繫結至由 Text 物件所呈現之 Button 控制項的 ButtonField 屬性。

DataTextFormatString

取得或設定字串,指定欄位值的顯示格式。

DesignMode

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

(繼承來源 DataControlField)
FooterStyle

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

(繼承來源 DataControlField)
FooterText

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

(繼承來源 DataControlField)
HeaderImageUrl

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

(繼承來源 DataControlField)
HeaderStyle

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

(繼承來源 DataControlField)
HeaderText

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

(繼承來源 DataControlField)
ImageUrl

取得或設定要顯示於 ButtonField 物件中之每個按鈕的影像。

InsertVisible

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

(繼承來源 DataControlField)
IsTrackingViewState

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

(繼承來源 DataControlField)
ItemStyle

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

(繼承來源 DataControlField)
ShowHeader

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

(繼承來源 ButtonFieldBase)
SortExpression

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

(繼承來源 DataControlField)
Text

取得或設定要顯示於 ButtonField 物件中之每個按鈕的靜態標題。

ValidateRequestMode

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

(繼承來源 DataControlField)
ValidationGroup

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

(繼承來源 ButtonFieldBase)
ViewState

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

(繼承來源 DataControlField)
Visible

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

(繼承來源 DataControlField)

方法

CloneField()

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

(繼承來源 DataControlField)
CopyProperties(DataControlField)

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

CreateField()

建立並傳回 ButtonField 類別的新執行個體。

Equals(Object)

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

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

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

(繼承來源 DataControlField)
FormatDataTextValue(Object)

格式化 ButtonField 物件中儲存格的指定欄位值。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

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

初始化目前的 ButtonField 物件。

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()

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

明確介面實作

IDataSourceViewSchemaAccessor.DataSourceViewSchema

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

(繼承來源 DataControlField)
IStateManager.IsTrackingViewState

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

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

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

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

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

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

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

(繼承來源 DataControlField)

適用於

另請參閱