2009-06-01 42 views
5

您只能在ListView中定義一個GroupItemCount,但是如果您想根據數據源中的項目屬性進行分組,那該怎麼辦?按特定組排序。數據源在此屬性上排序。是否可以在ASP.NET ListView控件中進行自定義分組?

我已經看到了一些示例,其中ItemTemplate中的某些標記是有條件顯示的,但如果可能的話我想利用GroupTemplate。

這可能嗎?

+0

順便說一句,我不是在尋找的代碼,只需要在正確的方向指針。 – 2009-06-01 19:55:43

回答

3

當我不得不爲轉發我在ItemTemplate Literal控件這樣做增加基本組標題:

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

在代碼中的「GetGroupHeading」方法持續跟蹤前一組標題和發送'<h2>組名稱</h2 >',或者如果我們與前一個項目位於同一組,則爲空字符串。正如我所說,我在一個Repeater上做了這個,所以不知道它是否會涵蓋ListView所需的內容。

+0

您也可以在ListView中做到這一點,但添加標題與ListView控件中GroupTemplate的分組不同。通過GroupTemplate,您可以在模板中用標記包圍項目。據我所知,除了所有你可以分組的數量。 – 2009-06-03 21:45:17

2

是尼克給了很大的領先優勢。這裏是我的代碼隱藏

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 

而且我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> 
相關問題