TemplateField.AlternatingItemTemplate 屬性

定義

取得或設定顯示 TemplateField 物件中替代項目的樣板。

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 實作的物件,包含顯示 TemplateField 中替代項目的樣板。 預設值為 null,表示這個屬性尚未設定。

屬性

範例

下列程式碼範例示範如何使用 AlternatingItemTemplate 屬性,為控制項欄位 GridView 資料行中的 TemplateField 替代專案建立自訂範本。 範本是 屬性所 ItemTemplate 指定的範本變化,其中影像會顯示在儲存格的相反端。


<%@ 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使用 屬性可指定物件中 TemplateField 替代專案所顯示的自訂內容。 藉由建立範本來定義內容,以指定如何轉譯替代專案。

注意

屬性 AlternatingItemTemplate 通常會與 屬性搭配 ItemTemplate 使用,以針對資料繫結控制項中的每個其他專案建立不同的外觀。

若要指定範本,請先在元素的開頭和結束記號之間放置開頭和結尾 <AlternatingItemTemplate> 標記 <TemplateField> 。 接下來,在開頭和結尾 <AlternatingItemTemplate> 標記之間新增自訂內容。 內容可以像純文字一樣簡單,或更複雜的 (在範本中內嵌其他控制項,例如) 。

若要以程式設計方式存取範本中定義的控制項,請先判斷資料繫結控制項中的哪個 TableCell 物件包含 控制項。 接下來,使用 Controls 物件的集合 TableCell 來存取 控制項。 如果控制項已 ID 指定屬性,您也可以使用 FindControl 物件的 方法來 TableCell 尋找控制項。

適用於

另請參閱