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; }
member this.AlternatingItemTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property AlternatingItemTemplate As 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属性为GridView控件的TemplateField字段列中的交替项创建自定义模板。The 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>

注解

使用属性指定为TemplateField对象中的交替项显示的自定义内容。 AlternatingItemTemplateUse 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 属性,则还可以使用对象的方法ID来查找控件。You can also use the FindControl method of the TableCell object to find the control, if the control has an ID property specified.

适用于

另请参阅