TemplateField 类

定义

表示在数据绑定控件中显示自定义内容的字段。Represents a field that displays custom content in a data-bound control.

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

示例

下面的代码示例演示如何创建一个自定义TemplateField对象, 该对象在同一单元格中显示两个字段。The following code example demonstrates how to create a custom TemplateField object that displays two fields in the same cell. "名字" 和 "姓氏" 字段合并为同一TemplateField对象。The first name and last name fields are combined into the same TemplateField object.


<%@ 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>TemplateField Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>TemplateField Example</h3>

      <!-- Populate the Columns collection declaratively. -->
      <!-- Create a custom TemplateField column that uses      -->
      <!-- two Label controls to display an author's first and -->
      <!-- last name in the same column.                       -->
      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="False"
        runat="server">
                
        <columns>
                
          <asp:templatefield headertext="Author Name">
            <itemtemplate>
              <asp:label id="FirstNameLabel"
                Text= '<%# Eval("au_fname") %>'
                runat="server"/> 
              <asp:label id="LastNameLabel"
                Text= '<%# Eval("au_lname") %>'
                runat="server"/>
            </itemtemplate>
          </asp:templatefield>
                
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_lname], [au_fname], [address], [city], [state], [zip], [contract] FROM [authors]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        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>TemplateField Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>TemplateField Example</h3>

      <!-- Populate the Columns collection declaratively. -->
      <!-- Create a custom TemplateField column that uses      -->
      <!-- two Label controls to display an author's first and -->
      <!-- last name in the same column.                       -->
      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="False"
        runat="server">
                
        <columns>
                
          <asp:templatefield headertext="Author Name">
            <itemtemplate>
              <asp:label id="FirstNameLabel"
                text= '<%# Eval("au_fname") %>'
                runat="server"/> 
              <asp:label id="LastNameLabel"
                text= '<%# Eval("au_lname") %>'
                runat="server"/>
            </itemtemplate>
          </asp:templatefield>
                
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_lname], [au_fname], [address], [city], [state], [zip], [contract] FROM [authors]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

注解

数据绑定控件 ( GridView如和DetailsView) 使用类来显示每个显示的记录的自定义内容。TemplateFieldThe TemplateField class is used by data-bound controls (such as GridView and DetailsView) to display custom content for each record displayed. 当你需要在未由预定义的数据控件字段 (如BoundField) 之一提供的数据绑定控件中显示内容时, 请TemplateField使用类来创建自定义用户界面 (UI)。When you need to display content in a data-bound control that is not provided by one of the predefined data control fields (such as BoundField), use the TemplateField class to create your custom user interface (UI). TemplateField对象的显示方式取决于使用它的数据绑定控件。The TemplateField object is displayed differently depending on the data-bound control in which it is used. 例如, GridView控件将TemplateField对象显示为DetailsView一列, 并且控件将其显示为一行。For example, the GridView control displays a TemplateField object as a column, and the DetailsView control displays it as a row.

您可以使用下表中列出的模板为TemplateField对象的不同部分定义自定义模板。You can define a custom template for the different parts of the TemplateField object using the templates listed in the following table.

模板Template 说明Description
AlternatingItemTemplate 指定要为TemplateField对象中的交替项显示的内容。Specifies the content to display for the alternating items in a TemplateField object.
EditItemTemplate 指定要为TemplateField对象中的编辑模式的项显示的内容。Specifies the content to display for an item in edit mode in a TemplateField object.
FooterTemplate TemplateField对象的脚注部分指定要显示的内容。Specifies the content to display for the footer section of a TemplateField object.
HeaderTemplate TemplateField对象的标头部分指定要显示的内容。Specifies the content to display for the header section of a TemplateField object.
InsertItemTemplate 指定为TemplateField对象中的插入模式项显示的内容。Specifies the content to display for an item in insert mode in a TemplateField object. 此模板仅DetailsView受控件支持。This template is supported only by the DetailsView control.
ItemTemplate 指定要为TemplateField对象中的项显示的内容。Specifies the content to display for the items in a TemplateField object.

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

作为定义自HeaderTemplate定义或FooterTemplate模板的替代方法, 您可以通过设置TemplateField对象的其他属性自定义TemplateField对象的页眉和页脚部分。As an alternative to defining a custom HeaderTemplate or FooterTemplate template, you can customize the header and footer sections of a TemplateField object by setting other properties of the TemplateField object. 若要在页眉或页脚部分显示标题, 请分别设置HeaderText FooterText或属性。To display a caption in the header or footer sections, set the HeaderText or the FooterText property, respectively. 您可以通过设置HeaderImageUrl属性来显示图像, 而不是在页眉节中显示文本。Instead of displaying text in the header section, you can display an image by setting the HeaderImageUrl property. 通过TemplateField false将属性设置为, 可以在对象中隐藏标头部分。 ShowHeaderThe header section can be hidden in the TemplateField 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, use the control's ShowHeader property (if available).

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

构造函数

TemplateField()

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

属性

AccessibleHeaderText

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

(继承自 DataControlField)
AlternatingItemTemplate

获取或设置用于显示 TemplateField 对象中交替项的模板。Gets or sets the template for displaying the alternating items in a TemplateField object.

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

获取或设置一个值,该值指示在 TemplateField 对象绑定到的值为 Empty 时是否应将其转换为 nullGets or sets a value indicating whether the value that the TemplateField object is bound to should be converted to null if it is Empty.

DesignMode

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

(继承自 DataControlField)
EditItemTemplate

获取或设置模板,该模板用于显示 TemplateField 对象中处于编辑模式中的项。Gets or sets the template for displaying an item in edit mode in a TemplateField object.

FooterStyle

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

(继承自 DataControlField)
FooterTemplate

获取或设置用于显示 TemplateField 对象的脚注部分的模板。Gets or sets the template for displaying the footer section of a TemplateField object.

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

获取或设置用于显示 TemplateField 对象的标头部分的模板。Gets or sets the template for displaying the header section of a TemplateField object.

HeaderText

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

(继承自 DataControlField)
InsertItemTemplate

获取或设置模板,该模板用于显示 TemplateField 对象中处于插入模式中的项。Gets or sets the template for displaying an item in insert mode in a TemplateField 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)
ItemTemplate

获取或设置用于显示数据绑定控件中的项的模板。Gets or sets the template for displaying an item in a data-bound control.

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

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

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)

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

CreateField()

创建一个新的 TemplateField 对象。Creates a new TemplateField object.

Equals(Object)

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

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

从当前表单元格提取由一个或多个双向绑定语句 (DataBind) 指定的数据控件字段的值,并将这些值添加到指定的 IOrderedDictionary 集合。Extracts the value of the data control fields as specified by one or more two-way binding statements (DataBind) from the current table cell and adds the values to the specified IOrderedDictionary collection.

GetHashCode()

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

(继承自 Object)
GetType()

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

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

为数据控件字段执行基础实例初始化。Performs basic instance initialization for a data control field.

(继承自 DataControlField)
InitializeCell(DataControlFieldCell, DataControlCellType, DataControlRowState, Int32)

将文本或控件添加到单元格的控件集合中。Adds text or controls to a cell's controls collection.

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

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

适用于

另请参阅