ButtonField 类

定义

表示数据绑定控件中以按钮形式显示的字段。Represents a field that is displayed as a button in a data-bound control.

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在控件中显示命令按钮的列。The following code example demonstrates how to use a ButtonField object to display a column of command buttons in a GridView control.


<%@ 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>

注解

数据绑定控件 ( GridView如和DetailsView) 使用类来为显示的每个记录显示一个按钮。ButtonFieldThe ButtonField class is used by data-bound controls (such as GridView and DetailsView) to display a button for each record that is displayed. ButtonField对象的显示方式取决于使用它的数据绑定控件。The ButtonField object is displayed differently depending on the data-bound control in which it is used. 例如, GridView控件以列的形式ButtonField显示对象, 而DetailsView控件则将其显示为一行。For example, the GridView control displays a ButtonField object as a column, while the DetailsView control displays it as a row.

单击按钮字段中的按钮将引发父数据绑定控件的命令事件。Clicking a button in a button field raises the command event of the parent data-bound control. 通过为命令事件提供事件处理程序, 可提供在单击命令按钮时要执行的自定义例程。You can provide a custom routine to perform when a command button is clicked by providing an event handler for the command event.

备注

控件引发事件,ItemCommandDetailsView控件引发事件。 RowCommand GridViewThe GridView control raises the RowCommand event, while the DetailsView control raises the ItemCommand event.

若要确定引发命令事件的记录的索引, 请使用CommandArgument传递到数据绑定控件的命令事件的事件参数的属性。To determine the index of the record that raises the command event, use the CommandArgument property of the event argument that is passed to the command event for the data-bound control. 类自动用适当的CommandArgument索引值填充属性。 ButtonFieldThe ButtonField class automatically populates the CommandArgument property with the appropriate index value.

若要指定要显示的按钮类型, 请使用ButtonType属性。To specify the type of button to display, use the ButtonType property. 显示链接或命令按钮时, 请使用Text属性指定要在按钮中显示的标题。When displaying a link or command button, use the Text property to specify the caption to display in the buttons.

备注

如果设置Text了属性, 则中的ButtonField所有按钮都将共享相同的标题。If you set the Text property, all buttons in a ButtonField share the same caption.

或者, 您可以将ButtonField对象绑定到数据源中的字段。Alternatively, you can bind the ButtonField object to a field in a data source. 这允许您为ButtonField对象中的按钮显示不同的标题。This allows you to display different captions for the buttons in the ButtonField object. 指定字段中的值用于按钮的文本标题。The values that are in the specified field are used for the text captions of the buttons. 设置属性将ButtonField对象绑定到数据源中的字段。 DataTextFieldSet the DataTextField property to bind a ButtonField object to a field in a data source.

显示图像按钮时, 使用ImageUrl属性指定要为ButtonField对象中的按钮显示的图像。When displaying an image button, use the ImageUrl property to specify the image to display for the buttons in the ButtonField object.

备注

ButtonField对象中的所有按钮共享同一图像。All buttons in a ButtonField object share the same image.

可以通过ButtonField Visible将属性设置为来false隐藏数据绑定控件中的对象。You can hide a ButtonField object in a data-bound control by setting the Visible property to false.

ButtonField对象允许您自定义其页眉和页脚部分。The ButtonField object allows you to customize its header and footer sections. 若要在页眉或页脚部分显示标题, 请分别设置HeaderTextFooterText属性。To display a caption in the header or footer section, set the HeaderText or FooterText properties, respectively. 您可以通过设置HeaderImageUrl属性来显示图像, 而不是在页眉节中显示文本。Instead of displaying text in the header section, you can display an image by setting the HeaderImageUrl property. 若要隐藏ButtonField对象中的标头部分, 请ShowHeader将属性false设置为。To hide the header section in a ButtonField object, set the ShowHeader property to false.

备注

某些数据绑定控件 (如GridView控件) 只能显示或隐藏控件的整个标题部分。Some data-bound controls (such as the GridView control) can show or hide only the entire header section of the control. 这些数据绑定控件不支持单个按钮字段ShowHeader的属性。These data-bound controls do not support the ShowHeader property for an individual button field. 若要显示或隐藏数据绑定控件 (如果可用) 的整个标头部分, 请使用控件ShowHeader的属性。To show or hide the entire header section of a data-bound control (if available), use the ShowHeader property for the control.

还可以通过设置字段的不同部分ButtonField的样式属性来自定义对象 (字体颜色、背景色等) 的外观。You also can customize the appearance of the ButtonField object (font color, background color, and so on) by setting the style properties for the different parts of the field. 下表列出了不同的样式属性。The following table lists the different style properties.

Style 属性Style property 的样式设置The style settings for
ControlStyle 的子 Web 服务器控件ButtonFieldThe child Web server controls of the ButtonField.
FooterStyle 的页脚部分ButtonFieldThe footer section of the ButtonField.
HeaderStyle 的标头部分ButtonFieldThe header section of the ButtonField.
ItemStyle 中的数据项ButtonFieldThe data items in the ButtonField.

构造函数

ButtonField()

初始化 ButtonField 类的新实例。Initializes a new instance of the ButtonField class.

属性

AccessibleHeaderText

获取或设置某些控件中呈现为 AbbreviatedText 属性值的文本。Gets or sets text that is rendered as the AbbreviatedText property value in some controls.

(继承自 DataControlField)
ButtonType

获取或设置要在按钮字段中显示的按钮类型。Gets or sets the button type to display in the button field.

(继承自 ButtonFieldBase)
CausesValidation

获取或设置一个值,该值指示在单击 ButtonFieldBase 对象中的按钮时是否执行验证。Gets or sets a value indicating whether validation is performed when a button in a ButtonFieldBase object is clicked.

(继承自 ButtonFieldBase)
CommandName

获取或设置一个字符串,该字符串表示当单击 ButtonField 对象中的按钮时要执行的操作。Gets or sets a string that represents the action to perform when a button in a ButtonField object is clicked.

Control

获取对数据控件的引用,该控件与 DataControlField 对象关联。Gets a reference to the data control that the DataControlField object is associated with.

(继承自 DataControlField)
ControlStyle

获取 DataControlField 对象所包含的任何 Web 服务器控件的样式。Gets the style of any Web server controls contained by the DataControlField object.

(继承自 DataControlField)
DataTextField

获取或设置数据字段的名称,该数据字段的值绑定到 Text 控件的 Button 属性,此控件由 ButtonField 对象呈现。Gets or sets the name of the data field for which the value is bound to the Text property of the Button control that is rendered by the ButtonField object.

DataTextFormatString

获取或设置字符串,该字符串指定字段值的显示格式。Gets or sets the string that specifies the display format for the value of the field.

DesignMode

获取一个值,该值指示数据控件字段当前是否在设计时环境中进行查看。Gets a value indicating whether a data control field is currently viewed in a design-time environment.

(继承自 DataControlField)
FooterStyle

获取或设置数据控件字段脚注的样式。Gets or sets the style of the footer of the data control field.

(继承自 DataControlField)
FooterText

获取或设置数据控件字段的脚注项中显示的文本。Gets or sets the text that is displayed in the footer item of a data control field.

(继承自 DataControlField)
HeaderImageUrl

获取或设置数据控件字段的标题项中显示的图像的 URL。Gets or sets the URL of an image that is displayed in the header item of a data control field.

(继承自 DataControlField)
HeaderStyle

获取或设置数据控件字段标头的样式。Gets or sets the style of the header of the data control field.

(继承自 DataControlField)
HeaderText

获取或设置数据控件字段的标题项中显示的文本。Gets or sets the text that is displayed in the header item of a data control field.

(继承自 DataControlField)
ImageUrl

获取或设置要为 ButtonField 对象中每个按钮显示的图像。Gets or sets the image to display for each button in the ButtonField object.

InsertVisible

获取一个值,该值指示 DataControlField 对象在其父级数据绑定控件处于插入模式时是否可见。Gets a value indicating whether the DataControlField object is visible when its parent data-bound control is in insert mode.

(继承自 DataControlField)
IsTrackingViewState

获取一个值,该值指示 DataControlField 对象是否保存对其视图状态的更改。Gets a value indicating whether the DataControlField object is saving changes to its view state.

(继承自 DataControlField)
ItemStyle

获取由数据控件字段显示的任何基于文本的内容的样式。Gets the style of any text-based content displayed by a data control field.

(继承自 DataControlField)
ShowHeader

获取或设置一个值,该值指示是否显示 ButtonFieldBase 对象的标头部分。Gets or sets a value indicating whether the header section is displayed in a ButtonFieldBase object.

(继承自 ButtonFieldBase)
SortExpression

获取或设置数据源控件用来对数据进行排序的排序表达式。Gets or sets a sort expression that is used by a data source control to sort data.

(继承自 DataControlField)
Text

获取或设置为 ButtonField 对象中每个按钮显示的静态标题。Gets or sets the static caption that is displayed for each button in the ButtonField object.

ValidateRequestMode

获取或设置一个值,该值指定该控件是否验证客户端输入。Gets or sets a value that specifies whether the control validates client input.

(继承自 DataControlField)
ValidationGroup

获取或设置在单击 ButtonFieldBase 对象中的按钮时要验证的验证控件组的名称。Gets or sets the name of the group of validation controls to validate when a button in a ButtonFieldBase object is clicked.

(继承自 ButtonFieldBase)
ViewState

获取状态信息的字典,这些信息使您可以在同一页的多个请求间保存和还原 DataControlField 对象的视图状态。Gets a dictionary of state information that allows you to save and restore the view state of a DataControlField object across multiple requests for the same page.

(继承自 DataControlField)
Visible

获取或设置指示是否呈现数据控件字段的值。Gets or sets a value indicating whether a data control field is rendered.

(继承自 DataControlField)

方法

CloneField()

创建当前 DataControlField 派生对象的副本。Creates a duplicate copy of the current DataControlField-derived object.

(继承自 DataControlField)
CopyProperties(DataControlField)

将当前 ButtonField 对象的属性复制到指定的 DataControlField 对象。Copies the properties of the current ButtonField object to the specified DataControlField object.

CreateField()

创建并返回 ButtonField 类的新实例。Creates and returns a new instance of the ButtonField class.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
ExtractValuesFromCell(IOrderedDictionary, DataControlFieldCell, DataControlRowState, Boolean)

从当前表格单元格中提取数据控件字段的值,并将该值添加到指定的 IDictionary 集合中。Extracts the value of the data control field from the current table cell and adds the value to the specified IDictionary collection.

(继承自 DataControlField)
FormatDataTextValue(Object)

设定 ButtonField 对象内的单元格中的指定字段值。Formats the specified field value for a cell in the ButtonField object.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
Initialize(Boolean, Control)

初始化当前的 ButtonField 对象。Initializes the current ButtonField object.

InitializeCell(DataControlFieldCell, DataControlCellType, DataControlRowState, Int32)

将指定的 DataControlFieldCell 对象初始化为指定的行状态。Initializes the specified DataControlFieldCell object to the specified row state.

LoadViewState(Object)

将数据源视图还原为保存过的前一视图状态。Restores the data source view's previously saved view state.

(继承自 DataControlField)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
OnFieldChanged()

引发 FieldChanged 事件。Raises the FieldChanged event.

(继承自 DataControlField)
SaveViewState()

保存在页回发到服务器后对 DataControlField 视图状态所做的更改。Saves the changes made to the DataControlField view state since the time the page was posted back to the server.

(继承自 DataControlField)
ToString()

返回表示此 DataControlField 对象的字符串。Returns a string that represents this DataControlField object.

(继承自 DataControlField)
TrackViewState()

使 DataControlField 对象跟踪对其视图状态所做的更改,以便这些更改可以存储在控件的 ViewState 属性中并且能够在同一页的不同请求间得以保持。Causes the DataControlField object to track changes to its view state so they can be stored in the control's ViewState property and persisted across requests for the same page.

(继承自 DataControlField)
ValidateSupportsCallback()

确定包含在 ButtonField 对象中的控件是否支持回调。Determines whether the controls that are contained in a ButtonField object support callbacks.

显式界面实现

IDataSourceViewSchemaAccessor.DataSourceViewSchema

获取或设置与此 DataControlField 对象关联的架构。Gets or sets the schema associated with this DataControlField object.

(继承自 DataControlField)
IStateManager.IsTrackingViewState

获取一个值,该值指示 DataControlField 对象是否保存对其视图状态的更改。Gets a value indicating whether the DataControlField object is saving changes to its view state.

(继承自 DataControlField)
IStateManager.LoadViewState(Object)

将数据控件字段还原为保存过的前一视图状态。Restores the data control field's previously saved view state.

(继承自 DataControlField)
IStateManager.SaveViewState()

保存在页回发到服务器后对 DataControlField 视图状态所做的更改。Saves the changes made to the DataControlField view state since the time the page was posted back to the server.

(继承自 DataControlField)
IStateManager.TrackViewState()

使 DataControlField 对象跟踪对其视图状态所做的更改,以便这些更改可以存储在控件的 ViewState 属性中并且能够在同一页的不同请求间得以保持。Causes the DataControlField object to track changes to its view state so they can be stored in the control's ViewState property and persisted across requests for the same page.

(继承自 DataControlField)

适用于

另请参阅