TemplateField.AlternatingItemTemplate 屬性

定義

取得或設定顯示 TemplateField 物件中替代項目的樣板。Gets or sets the template for displaying the alternating items in a TemplateField object.

public:
 virtual property System::Web::UI::ITemplate ^ AlternatingItemTemplate { System::Web::UI::ITemplate ^ get(); void set(System::Web::UI::ITemplate ^ value); };
[System.ComponentModel.Browsable(false)]
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
[System.Web.UI.TemplateContainer(typeof(System.Web.UI.IDataItemContainer), System.ComponentModel.BindingDirection.TwoWay)]
public virtual System.Web.UI.ITemplate AlternatingItemTemplate { get; set; }
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
[<System.Web.UI.TemplateContainer(typeof(System.Web.UI.IDataItemContainer), System.ComponentModel.BindingDirection.TwoWay)>]
member this.AlternatingItemTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property AlternatingItemTemplate As ITemplate

屬性值

ITemplate

ITemplate 實作的物件,包含顯示 TemplateField 中替代項目的樣板。A ITemplate-implemented object that contains the template for displaying the alternating items in a TemplateField. 預設值為 null,表示這個屬性尚未設定。The default is null, which indicates that this property is not set.

屬性

範例

下列程式碼範例示範如何使用 AlternatingItemTemplate 屬性,針對 TemplateField 控制項之欄位資料行中的替代專案建立自訂範本 GridViewThe following code example demonstrates how to use the AlternatingItemTemplate property to create a custom template for the alternating items in a TemplateField field column of a GridView control. 此範本是屬性所指定之範本的變化 ItemTemplate ,其中的影像會顯示在資料格的另一邊。The template is a variation of the template specified by the ItemTemplate property, where an image is displayed on the opposite side of the cell.


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

      <!-- Populate the Columns collection declaratively.    -->
      <!-- Create a TemplateField field column that has both      -->
      <!-- an  item template and an alternating item template.    -->
      <!-- The item template displays an author's image on the    -->
      <!-- left side of the column, while the alternating item    -->
      <!-- template displays an author's image on the right side. -->
      
      <!-- For this example, the zip field is used for the        -->
      <!-- values of the image URL. For your application, you     -->
      <!-- should use a field that contains valid URLs to         -->
      <!-- images.                                                -->
      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="False"
        runat="server">
                
        <columns>
                
          <asp:templatefield headertext="Author">
            <itemtemplate>
              <asp:image id="LeftAuthorImage"
                imageurl='<%# Eval("zip") %>'
                alternatetext="Author Photo"  
                runat="server"/>
              <asp:label id="LeftFirstNameLabel"
                text= '<%# Eval("au_fname") %>'
                runat="server"/> 
              <asp:label id="LeftLastNameLabel"
                text= '<%# Eval("au_lname") %>'
                runat="server"/>
            </itemtemplate>
            <alternatingitemtemplate>
              <asp:label id="RightFirstNameLabel"
                text= '<%# Eval("au_fname") %>'
                runat="server"/> 
              <asp:label id="RightLastNameLabel"
                text= '<%# Eval("au_lname") %>'
                runat="server"/>
              <asp:image id="RightAuthorImage"
                imageurl='<%# Eval("zip") %>'
                alternatetext="Author Photo"
                runat="server"/>
            </alternatingitemtemplate>
          </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], [zip] 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 AlternatingItemTemplate Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>TemplateField AlternatingItemTemplate Example</h3>

      <!-- Populate the Columns collection declaratively.    -->
      <!-- Create a TemplateField field column that has both      -->
      <!-- an  item template and an alternating item template.    -->
      <!-- The item template displays an author's image on the    -->
      <!-- left side of the column, while the alternating item    -->
      <!-- template displays an author's image on the right side. -->
      
      <!-- For this example, the zip field is used for the        -->
      <!-- values of the image URL. For your application, you     -->
      <!-- should use a field that contains valid URLs to         -->
      <!-- images.                                                -->
      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="False"
        runat="server">
                
        <columns>
                
          <asp:templatefield headertext="Author">
            <itemtemplate>
              <asp:image id="LeftAuthorImage"
                imageurl='<%# Eval("zip") %>'
                alternatetext="Author Photo"  
                runat="server"/>
              <asp:label id="LeftFirstNameLabel"
                text= '<%# Eval("au_fname") %>'
                runat="server"/> 
              <asp:label id="LeftLastNameLabel"
                text= '<%# Eval("au_lname") %>'
                runat="server"/>
            </itemtemplate>
            <alternatingitemtemplate>
              <asp:label id="RightFirstNameLabel"
                text= '<%# Eval("au_fname") %>'
                runat="server"/> 
              <asp:label id="RightLastNameLabel"
                text= '<%# Eval("au_lname") %>'
                runat="server"/>
              <asp:image id="RightAuthorImage"
                imageurl='<%# Eval("zip") %>'
                alternatetext="Author Photo"
                runat="server"/>
            </alternatingitemtemplate>
          </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], [zip] FROM [authors]"
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

備註

AlternatingItemTemplate 可以使用屬性來指定針對物件中的替代專案所顯示的自訂內容 TemplateFieldUse the AlternatingItemTemplate property to specify the custom content displayed for the alternating items in a TemplateField object. 藉由建立可指定替換專案呈現方式的範本來定義內容。Define the content by creating a template that specifies how the alternating items are rendered.

注意

AlternatingItemTemplate屬性通常會與屬性一起使用 ItemTemplate ,為資料繫結控制項中的每個其他專案建立不同的外觀。The AlternatingItemTemplate property is commonly used in combination with the ItemTemplate property to create a different appearance for every other item in the data-bound control.

若要指定範本,請先在 <AlternatingItemTemplate> 元素的開頭和結束記號之間放置開頭和結束記號 <TemplateField>To specify a template, first place opening and closing <AlternatingItemTemplate> tags between the opening and closing tags of the <TemplateField> element. 接下來,在開頭和結束記號之間加入自訂內容 <AlternatingItemTemplate>Next, add the custom content between the opening and closing <AlternatingItemTemplate> tags. 內容可以像純文字一樣簡單,也可以是更複雜的 (內嵌範本中的其他控制項,例如) 。The content can be as simple as plain text or more complex (embedding other controls in the template, for example).

若要以程式設計方式存取在範本中定義的控制項,請先判斷 TableCell 資料繫結控制項中的哪一個物件包含控制項。To programmatically access a control defined in a template, first determine which TableCell object in the data-bound control contains the control. 接下來,使用 Controls 物件的集合 TableCell 來存取控制項。Next, use the Controls collection of the TableCell object to access the control. FindControl TableCell 如果控制項有指定的屬性,您也可以使用物件的方法來尋找控制項 IDYou can also use the FindControl method of the TableCell object to find the control, if the control has an ID property specified.

適用於

另請參閱