ListView.GroupItemCount 屬性

定義

取得或設定 ListView 控制項中每一個群組所顯示的項目數目。

public:
 virtual property int GroupItemCount { int get(); void set(int value); };
public virtual int GroupItemCount { get; set; }
member this.GroupItemCount : int with get, set
Public Overridable Property GroupItemCount As Integer

屬性值

ListView 控制項中每一個群組所顯示的項目數目。 預設為 1,表示 ListView 控制項會在每一個群組中顯示一個項目。

例外狀況

GroupItemCount 屬性會設定為小於 1 的值。

範例

下列範例示範如何使用 屬性, ListView 在 控制項 GroupItemCount 中建立磚版面配置。 它也會示範如何以程式設計方式變更此屬性。

<%@ Page language="C#" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void GroupItemCountList_SelectedIndexChanged(object sender, EventArgs e)
    {
      // Changes the number of items displayed for each group.
      ContactsListView.GroupItemCount = 
        Convert.ToInt32(GroupItemCountList.SelectedValue);
    }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView GroupItemCount Example</title>
    <style type="text/css">
      body { font: 10pt Trebuchet MS; }        
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListView GroupItemCount Example</h3>

      Number of columns:
      <asp:DropDownList ID="GroupItemCountList"
        AutoPostBack="true"
        runat="server"         
        OnSelectedIndexChanged="GroupItemCountList_SelectedIndexChanged"        
        >
          <asp:ListItem>1</asp:ListItem>
          <asp:ListItem Selected="True">2</asp:ListItem>
          <asp:ListItem>3</asp:ListItem>
          <asp:ListItem>4</asp:ListItem>
      </asp:DropDownList>
      <br />
      <br />      
      <hr />
      
      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        GroupItemCount="2"        
        runat="server">
        <LayoutTemplate>
          <table id="tblContacts" runat="server" cellspacing="0" cellpadding="2">
            <tr runat="server" id="groupPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="ContactsDataPager" PageSize="24">
            <Fields>
              <asp:NextPreviousPagerField 
                ShowFirstPageButton="true" ShowLastPageButton="true"
                FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <td align="center" style="width:10px" runat="server">
            <asp:Label ID="IDLabel" runat="Server" Text='<%#Eval("ContactID") %>' />
          </td>
          <td style="width:70px" runat="server">
            <asp:Label ID="FirstNameLabel" runat="Server" Text='<%#Eval("FirstName") %>' />
          </td>
          <td style="width:70px" runat="server">
            <asp:Label ID="LastNameLabel" runat="Server" Text='<%#Eval("LastName") %>' />
          </td>
        </ItemTemplate>
        <GroupTemplate>
          <tr runat="server" id="ContactsRow" style="background-color: #FFFFFF">
            <td runat="server" id="itemPlaceholder" />
          </tr>
        </GroupTemplate>
        <ItemSeparatorTemplate>
          <td runat="server" style="border-right: 1px solid #00C0C0">&nbsp;</td>
        </ItemSeparatorTemplate>
      </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="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName] FROM Person.Contact">
      </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">

<script runat="server">

  Protected Sub GroupItemCountList_SelectedIndexChanged(ByVal sender As Object, _
    ByVal e As EventArgs)
          
    ' Changes the number of items displayed for each group.  
    ContactsListView.GroupItemCount = _
      Convert.ToInt32(GroupItemCountList.SelectedValue)
        
  End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView GroupItemCount Example</title>
    <style type="text/css">
      body { font: 10pt Trebuchet MS; }        
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListView GroupItemCount Example</h3>

      Number of columns:
      <asp:DropDownList ID="GroupItemCountList"
        AutoPostBack="true"
        runat="server"         
        OnSelectedIndexChanged="GroupItemCountList_SelectedIndexChanged"        
        >
          <asp:ListItem>1</asp:ListItem>
          <asp:ListItem Selected="True">2</asp:ListItem>
          <asp:ListItem>3</asp:ListItem>
          <asp:ListItem>4</asp:ListItem>
      </asp:DropDownList>
      <br />
      <br />      
      <hr />
      
      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        GroupItemCount="2"        
        runat="server">
        <LayoutTemplate>
          <table id="tblContacts" runat="server" cellspacing="0" cellpadding="2">
            <tr runat="server" id="groupPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="ContactsDataPager" PageSize="24">
            <Fields>
              <asp:NextPreviousPagerField 
                ShowFirstPageButton="true" ShowLastPageButton="true"
                FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <td align="center" style="width:10px" runat="server">
            <asp:Label ID="IDLabel" runat="Server" Text='<%#Eval("ContactID") %>' />
          </td>
          <td style="width:70px" runat="server">
            <asp:Label ID="FirstNameLabel" runat="Server" Text='<%#Eval("FirstName") %>' />
          </td>
          <td style="width:70px" runat="server">
            <asp:Label ID="LastNameLabel" runat="Server" Text='<%#Eval("LastName") %>' />
          </td>
        </ItemTemplate>
        <GroupTemplate>
          <tr runat="server" id="ContactsRow" style="background-color: #FFFFFF">
            <td runat="server" id="itemPlaceholder" />
          </tr>
        </GroupTemplate>
        <ItemSeparatorTemplate>
          <td runat="server" style="border-right: 1px solid #00C0C0">&nbsp;</td>
        </ItemSeparatorTemplate>
      </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="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName] FROM Person.Contact">
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>

備註

GroupItemCount使用 屬性指定要在 控制項中 ListView 群組在一起的專案數目。 (換句話說,這會指定要顯示控制項每個群組的專案數。) 針對每個群組, ListView 控制項會轉譯範本的內容 GroupTemplate ,並將專案預留位置取代為實際專案內容。 其執行此動作的次數與 屬性所 GroupItemCount 定義的專案數目一樣多。

您可以使用 GroupItemCountGroupTemplate 範本,在 控制項中 ListView 建立磚版面配置。 在圖格式表格配置中,專案會水準重複于資料列中。 屬性會 GroupItemCount 指定重複專案的次數。

適用於

另請參閱