Is it possible to do custom grouping in the ASP.NET ListView control?

前端 未结 3 920
无人共我
无人共我 2021-01-18 06:27

You can only define a GroupItemCount in the ListView, but what if you want to do grouping based on a property of the items in the data source? Sort of an ad-hoc group by. Th

相关标签:
3条回答
  • 2021-01-18 06:58

    When I had to add basic group headings in a repeater I did so with a Literal control in the ItemTemplate:

    <asp:Literal runat="server" Text='<%# GetGroupHeading(Eval("Group")) %>' />
    

    The 'GetGroupHeading' method in the code kept track of the previous group heading and sent back '<h2>Group Name</h2>', or an empty string if we were on the same group as the previous item. As I said though, I did this on a Repeater, so not sure if it will cover what you need for a ListView.

    0 讨论(0)
  • 2021-01-18 07:09

    Yes Nick gave a great lead. Here's my code-behind

    Dim sCategory_Descr As String
    Function GetGroupHeading(ByVal sGroupName As String) As String
        Dim sReturn As String
        If sCategory_Descr <> sGroupName Then
            sCategory_Descr = sGroupName
            sReturn = "<H5>Category: " & UCase(sGroupName) & "</H5>"
        Else
            sReturn = ""
        End If
            Return sReturn
    End Function 
    

    And my item_template

    <ItemTemplate>                    
        <tr>
            <td style="background-color:#ccc;" colspan="2" id="tdCategory_Placeholder" runat="server" >
                <asp:Label Font-Bold="true"  ID="Literal1" runat="server" Text='<%# GetGroupHeading(Eval("Category_Descr")) %>' /> 
            </td>
        </tr>                                     
        <tr>
            <td > 
                <asp:DynamicControl1 />                     
            </td>                                               
            <td > 
                <asp:DynamicControl2 />
            </td>                     
        </tr>
    </ItemTemplate>
    
    0 讨论(0)
  • 2021-01-18 07:23

    Try this article from 4 Guys from Rolla: Using ASP.NET 3.5's ListView and DataPager Controls: Grouping Data with the ListView Control

    0 讨论(0)
提交回复
热议问题