共用方式為


ListView.AlternatingItemTemplate 屬性

定義

取得或設定 ListView 控制項中替代資料項目的自訂內容。

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.WebControls.ListViewDataItem), 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.WebControls.ListViewDataItem), System.ComponentModel.BindingDirection.TwoWay)>]
member this.AlternatingItemTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property AlternatingItemTemplate As ITemplate

屬性值

物件,包含 ListView 控制項中替代資料項目的自訂內容。 預設值為 null,表示這個屬性尚未設定。

屬性

範例

下列範例示範如何使用 AlternatingItemTemplate 範本來定義 控件中替代數據項的 ListView 樣式。

<%@ 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 id="Head1" runat="server">
    <title>ListView AlternatingItemTemplate Example</title>
    <style type="text/css">
        body   
        {
          font-size: 10pt;
            font-family: Trebuchet MS, Arial, Tahoma;
            text-align:center;
        }
        .item { background-color: #E0FFFF }
        .alternatingItem { background-color: #B0E0E6 }
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
      
      <h3>ListView AlternatingItemTemplate Example</h3>
      
      <asp:ListView ID="VendorsListView" 
        DataSourceID="VendorsDataSource"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" width="640px" id="tblVendors" runat="server">
            <tr runat="server">
              <th runat="server">ID</th>
              <th runat="server">Account Number</th>
              <th runat="server">Name</th>
              <th runat="server">Preferred Vendor</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager ID="VendorsDataPager" runat="server" PageSize="15">
            <Fields>
              <asp:NumericPagerField ButtonCount="10" />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr class="item" runat="server">
            <td>
              <asp:Label ID="VendorIDLabel" runat="server" Text='<%# Eval("VendorID") %>' />
            </td>
            <td>
              <asp:Label ID="AccountNumberLabel" runat="server" Text='<%# Eval("AccountNumber") %>' />
            </td>
            <td>
              <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
            </td>
            <td align="center">
              <asp:CheckBox ID="PreferredCheckBox" runat="server" Enabled="False"
                Checked='<%# Eval("PreferredVendorStatus") %>' />
            </td>
          </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
          <tr class="alternatingItem" runat="server">
            <td>
              <asp:Label ID="VendorIDLabel" runat="server" Text='<%# Eval("VendorID") %>' />
            </td>
            <td>
              <asp:Label ID="AccountNumberLabel" runat="server" Text='<%# Eval("AccountNumber") %>' />
            </td>
            <td>
              <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
            </td>
            <td align="center">
              <asp:CheckBox ID="PreferredCheckBox" runat="server" Enabled="False"
                Checked='<%# Eval("PreferredVendorStatus") %>' />
            </td>
          </tr>
        </AlternatingItemTemplate>
      </asp:ListView>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="VendorsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT VendorID, AccountNumber, Name, PreferredVendorStatus 
          FROM Purchasing.Vendor WHERE (ActiveFlag = 1)" >
      </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 id="Head1" runat="server">
    <title>ListView AlternatingItemTemplate Example</title>
    <style type="text/css">
        body   
        {
          font-size: 10pt;
            font-family: Trebuchet MS, Arial, Tahoma;
            text-align:center;
        }
        .item { background-color: #E0FFFF }
        .alternatingItem { background-color: #B0E0E6 }
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
      
      <h3>ListView AlternatingItemTemplate Example</h3>
      
      <asp:ListView ID="VendorsListView" 
        DataSourceID="VendorsDataSource"
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" width="640px" id="tblVendors" runat="server">
            <tr runat="server">
              <th runat="server">ID</th>
              <th runat="server">Account Number</th>
              <th runat="server">Name</th>
              <th runat="server">Preferred Vendor</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager ID="VendorsDataPager" runat="server" PageSize="15">
            <Fields>
              <asp:NumericPagerField ButtonCount="10" />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr class="item" runat="server">
            <td>
              <asp:Label ID="VendorIDLabel" runat="server" Text='<%# Eval("VendorID") %>' />
            </td>
            <td>
              <asp:Label ID="AccountNumberLabel" runat="server" Text='<%# Eval("AccountNumber") %>' />
            </td>
            <td>
              <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
            </td>
            <td align="center">
              <asp:CheckBox ID="PreferredCheckBox" runat="server" Enabled="False"
                Checked='<%# Eval("PreferredVendorStatus") %>' />
            </td>
          </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
          <tr class="alternatingItem" runat="server">
            <td>
              <asp:Label ID="VendorIDLabel" runat="server" Text='<%# Eval("VendorID") %>' />
            </td>
            <td>
              <asp:Label ID="AccountNumberLabel" runat="server" Text='<%# Eval("AccountNumber") %>' />
            </td>
            <td>
              <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
            </td>
            <td align="center">
              <asp:CheckBox ID="PreferredCheckBox" runat="server" Enabled="False"
                Checked='<%# Eval("PreferredVendorStatus") %>' />
            </td>
          </tr>
        </AlternatingItemTemplate>
      </asp:ListView>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="VendorsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT VendorID, AccountNumber, Name, PreferredVendorStatus 
          FROM Purchasing.Vendor WHERE (ActiveFlag = 1)" >
      </asp:SqlDataSource>
    </form>
  </body>
</html>

備註

AlternatingItemTemplate使用 屬性,為替代的數據項定義自定義使用者介面 (UI) 。 範本 AlternatingItemTemplate 通常包含與範本相同的控件和內容 ItemTemplate ,但有不同的外觀來區分專案。

若要以宣告方式指定自定義範本,請在控件內ListView新增 專案AlternatingItemTemplate。 然後在開頭和結尾 <AlternatingItemTemplate> 標記之間新增控件和內容。 若要顯示數據源中的域值,請使用數據系結運算式。 如需詳細資訊,請參閱 數據系結表達式概觀

若要建立自動選取、刪除和編輯作業的按鈕,請將按鈕控件新增至範本。 將其 CommandName 屬性設定為下表所列的其中一個值。

按鈕類型 CommandName 值
刪除 "Delete"
編輯 “編輯”
選取 "Select"

適用於

另請參閱