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>

備註

TemplateField 類別是由資料繫結控制項所使用 (例如 GridViewDetailsView) ,以顯示所顯示之每筆記錄的自訂內容。The 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 指定要針對物件中的替代專案顯示的內容 TemplateFieldSpecifies the content to display for the alternating items in a TemplateField object.
EditItemTemplate 指定要在物件的編輯模式中顯示的專案內容 TemplateFieldSpecifies the content to display for an item in edit mode in a TemplateField object.
FooterTemplate 指定要針對物件的頁尾區段顯示的內容 TemplateFieldSpecifies the content to display for the footer section of a TemplateField object.
HeaderTemplate 指定要為物件的標頭區段顯示的內容 TemplateFieldSpecifies the content to display for the header section of a TemplateField object.
InsertItemTemplate 指定在物件的插入模式中,針對專案顯示的內容 TemplateFieldSpecifies the content to display for an item in insert mode in a TemplateField object. 此範本僅受到控制項的支援 DetailsViewThis template is supported only by the DetailsView control.
ItemTemplate 指定要為物件中的專案顯示的內容 TemplateFieldSpecifies the content to display for the items in a TemplateField object.

您可以藉 TemplateField 由將屬性設定為,來隱藏資料繫結控制項中的物件 Visible falseYou can hide a TemplateField object in a data-bound control by setting the Visible property to false.

除了定義自訂 HeaderTemplateFooterTemplate 範本外,您還可以藉 TemplateField 由設定物件的其他屬性來自訂物件的頁首和頁尾區段 TemplateFieldAs 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. 您可以藉由設定屬性來顯示影像,而不是在標頭區段中顯示文字 HeaderImageUrlInstead of displaying text in the header section, you can display an image by setting the HeaderImageUrl property. TemplateField 屬性設定為,即可在物件中隱藏標頭區段 ShowHeader falseThe 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 物件之子 Web 服務器控制項的樣式設定 TemplateFieldThe style settings for the child Web server controls of the TemplateField object.
FooterStyle 物件頁尾區段的樣式設定 TemplateFieldThe style settings for the footer section of the TemplateField object.
HeaderStyle 物件之標頭區段的樣式設定 TemplateFieldThe style settings for the header section of the TemplateField object.
ItemStyle 物件中資料項目的樣式設定 TemplateFieldThe 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 物件繫結至的值轉換成 null (如果其為 Empty)。Gets 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.

ValidateRequestMode

取得或設定值,這個值會指定控制項是否驗證用戶端輸入。Gets or sets a value that specifies whether the control validates client input.

(繼承來源 DataControlField)
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)

適用於

另請參閱