BoundField 类

定义

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

public ref class BoundField : System::Web::UI::WebControls::DataControlField
public class BoundField : System.Web.UI.WebControls.DataControlField
type BoundField = class
    inherit DataControlField
Public Class BoundField
Inherits DataControlField
继承
派生

示例

下面的代码示例演示如何使用BoundField对象GridView在控件中显示字段的值。The following code example demonstrates how to use a BoundField object to display the values of a field 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>BoundField Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>BoundField Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        autogenerateeditbutton="true"
        allowpaging="true" 
        datakeynames="CustomerID"  
        runat="server">
         
        <columns>
          <asp:boundfield datafield="CustomerID"
            readonly="true"      
            headertext="Customer ID"/>
          <asp:boundfield datafield="CompanyName"
            convertemptystringtonull="true"
            headertext="Customer Name"/>
          <asp:boundfield datafield="Address"
            convertemptystringtonull="true"
            headertext="Address"/>
          <asp:boundfield datafield="City"
            convertemptystringtonull="true"
            headertext="City"/>
          <asp:boundfield datafield="PostalCode"
            convertemptystringtonull="true"
            headertext="ZIP Code"/>
          <asp:boundfield datafield="Country"
            convertemptystringtonull="true"
            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>BoundField Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>BoundField Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        autogenerateeditbutton="true"
        allowpaging="true" 
        datakeynames="CustomerID"  
        runat="server">
         
        <columns>
          <asp:boundfield datafield="CustomerID"
            readonly="true"      
            headertext="Customer ID"/>
          <asp:boundfield datafield="CompanyName"
            convertemptystringtonull="true"
            headertext="Customer Name"/>
          <asp:boundfield datafield="Address"
            convertemptystringtonull="true"
            headertext="Address"/>
          <asp:boundfield datafield="City"
            convertemptystringtonull="true"
            headertext="City"/>
          <asp:boundfield datafield="PostalCode"
            convertemptystringtonull="true"
            headertext="ZIP Code"/>
          <asp:boundfield datafield="Country"
            convertemptystringtonull="true"
            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>

注解

数据绑定控件 ( GridView如和DetailsView) 使用类将字段的值显示为文本。BoundFieldThe BoundField class is used by data-bound controls (such as GridView and DetailsView) to display the value of a field as text. BoundField对象的显示方式取决于使用它的数据绑定控件。The BoundField object is displayed differently depending on the data-bound control in which it is used. 例如, GridView控件以列的形式BoundField显示对象, 而DetailsView控件则将其显示为一行。For example, the GridView control displays a BoundField object as a column, while the DetailsView control displays it as a row.

若要指定要在BoundField对象中显示的字段, 请DataField将属性设置为字段的名称。To specify the field to display in a BoundField object, set the DataField property to the field's name. 通过将HtmlEncode属性设置为, true可以在显示字段的值之前对其进行 HTML 编码。The field's value can be HTML-encoded before it is displayed by setting the HtmlEncode property to true. 可以通过设置DataFormatString属性, 将自定义格式设置字符串应用于字段的值。You can apply a custom formatting string to the field's value by setting the DataFormatString property. 当属性为true时, 将在自定义格式字符串中使用字段的已编码字符串值。 HtmlEncodeWhen the HtmlEncode property is true, the encoded string value of the field is used in the custom format string. 默认情况下, 仅当数据绑定控件处于只读模式时, 才将格式设置字符串应用于字段值。By default, the formatting string is applied to field values only when the data-bound control is in read-only mode. 若要将格式设置字符串应用于数据绑定控件处于编辑模式时显示的值, 请将ApplyFormatInEditMode属性设置true为。To apply the formatting string to values displayed while the data-bound control is in edit mode, set the ApplyFormatInEditMode property to true. 如果字段值为 null, 则可以通过设置NullDisplayText属性来显示自定义标题。If a field's value is null, you can display a custom caption by setting the NullDisplayText property. 对象也可以通过ConvertEmptyStringToNull将属性设置为来true自动将空字符串 ("") 字段值转换为 null 值。 BoundFieldThe BoundField object can also automatically convert empty string ("") field values to null values by setting the ConvertEmptyStringToNull property to true.

可以通过BoundField Visible将属性设置为来false隐藏数据绑定控件中的对象。You can hide a BoundField object in a data-bound control by setting the Visible property to false. 若要防止在编辑模式下修改字段的值, 请将ReadOnly属性设置为。 trueTo prevent the value of a field from being modified in edit mode, set the ReadOnly property to true. 在支持插入记录的数据绑定控件DetailsView (如控件) 中, 可以通过将InsertVisible属性设置为BoundFieldfalse隐藏对象。In data-bound controls that support inserting records (such as the DetailsView control), you can hide a BoundField object by setting the InsertVisible property to false. 当您希望在插入模式下隐藏自动生成的键字段时, 通常会执行此操作。This is commonly done when you want to hide an automatically generated key field in insert mode.

您可以自定义BoundField对象的页眉和页脚部分。You can customize the header and footer sections of a BoundField object. 若要在页眉或页脚部分显示标题, 请分别设置HeaderTextFooterText属性。To display a caption in the header or footer sections, 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. 通过BoundField false将属性设置为, 可以在对象中隐藏标头部分。 ShowHeaderThe header section can be hidden in the BoundField object by setting 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 bound field. 若要显示或隐藏数据绑定控件 (如果可用) 的整个标头部分, 请使用控件的ShowHeader属性。To show or hide the entire header section of a data-bound control (if available), use the control's ShowHeader property.

还可以通过设置字段的不同部分BoundField的样式属性来自定义对象 (字体颜色、背景色等) 的外观。You also can customize the appearance of the BoundField 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 说明Description
ControlStyle BoundField对象的子 Web 服务器控件的样式设置。The style settings for the child Web server controls of the BoundField object.
FooterStyle BoundField对象的脚注部分的样式设置。The style settings for the footer section of the BoundField object.
HeaderStyle BoundField对象的标头部分的样式设置。The style settings for the header section of the BoundField object.
ItemStyle BoundField对象中数据项的样式设置。The style settings for the data items in the BoundField object.

构造函数

BoundField()

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

字段

ThisExpression

表示“this”表达式。Represents the "this" expression.

属性

AccessibleHeaderText

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

(继承自 DataControlField)
ApplyFormatInEditMode

获取或设置一个值,该值指示包含 DataFormatString 对象的数据绑定控件处于编辑模式时,BoundField 属性指定的格式化字符串是否应用到字段值。Gets or sets a value indicating whether the formatting string specified by the DataFormatString property is applied to field values when the data-bound control that contains the BoundField object is in edit mode.

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

获取或设置一个值,该值指示在数据源中更新数据字段时是否将空字符串值 ("") 自动转换为 null 值。Gets or sets a value indicating whether empty string values ("") are automatically converted to null values when the data field is updated in the data source.

DataField

获取或设置要绑定到 BoundField 对象的数据字段的名称。Gets or sets the name of the data field to bind to the BoundField object.

DataFormatString

获取或设置字符串,该字符串指定字段值的显示格式。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 of a data control.

HtmlEncode

获取或设置一个值,该值指示在 BoundField 对象中显示字段值之前,是否对这些字段值进行 HTML 编码。Gets or sets a value indicating whether field values are HTML-encoded before they are displayed in a BoundField object.

HtmlEncodeFormatString

获取或设置一个值,该值指示通过将 DataFormatString 属性应用于 BoundField 值而创建的文本在显示时是否应经过 HTML 编码。Gets or sets a value that indicates whether text that is created by applying the DataFormatString property to the BoundField value should be HTML encoded when it is displayed.

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

获取或设置当字段值为 null 时为字段显示的标题。Gets or sets the caption displayed for a field when the field's value is null.

ReadOnly

获取或设置一个值,该值指示是否可以在编辑模式中修改字段的值。Gets or sets a value indicating whether the value of the field can be modified in edit mode.

ShowHeader

获取或设置一个值,该值指示是否呈现数据控件字段的标题项。Gets or sets a value indicating whether the header item of a data control field is rendered.

(继承自 DataControlField)
SortExpression

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

(继承自 DataControlField)
SupportsHtmlEncode

获取一个值,该值指示 BoundField 对象是否支持 HTML 编码。Gets a value indicating whether HTML encoding is supported by a BoundField object.

ValidateRequestMode

获取或设置确定控件是否验证客户端输入的值。Gets or sets a value that determines whether the control validates client input or not.

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)

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

CreateField()

创建一个空的 BoundField 对象。Creates an empty BoundField object.

Equals(Object)

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

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

使用指定的 IDictionary 对象中的值填充指定的 TableCell 对象。Fills the specified IDictionary object with the values from the specified TableCell object.

FormatDataValue(Object, Boolean)

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

GetDesignTimeValue()

在设计器中呈现 BoundField 对象时,检索用作字段值的值。Retrieves the value used for a field's value when rendering the BoundField object in a designer.

GetHashCode()

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

(继承自 Object)
GetType()

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

(继承自 Object)
GetValue(Control)

检索绑定到 BoundField 对象的字段值。Retrieves the value of the field bound to the BoundField object.

Initialize(Boolean, Control)

初始化 BoundField 对象。Initializes the BoundField object.

InitializeCell(DataControlFieldCell, DataControlCellType, DataControlRowState, Int32)

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

InitializeDataCell(DataControlFieldCell, DataControlRowState)

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

LoadViewState(Object)

还原以前为此字段存储的视图状态信息。Restores the previously stored view-state information for this field.

MemberwiseClone()

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

(继承自 Object)
OnDataBindField(Object, EventArgs)

将字段值绑定到 BoundField 对象。Binds the value of a field to the BoundField 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()

确定包含在 BoundField 对象中的控件是否支持回调。Determines whether the controls contained in a BoundField 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)

适用于

另请参阅