【发布时间】:2011-09-12 23:12:31
【问题描述】:
你只能在ListView中定义一个GroupItemCount,但是如果你想根据数据源中item的一个属性进行分组呢?有点像临时组。数据源按此属性排序。
我看到了一些示例,其中有条件地显示 ItemTemplate 中的某些标记,但如果可能,我想利用 GroupTemplate。
这可能吗?
【问题讨论】:
-
顺便说一句,我不是在找代码,只是一个指向正确方向的指针。
你只能在ListView中定义一个GroupItemCount,但是如果你想根据数据源中item的一个属性进行分组呢?有点像临时组。数据源按此属性排序。
我看到了一些示例,其中有条件地显示 ItemTemplate 中的某些标记,但如果可能,我想利用 GroupTemplate。
这可能吗?
【问题讨论】:
当我必须在转发器中添加基本组标题时,我使用 ItemTemplate 中的 Literal 控件完成了此操作:
<asp:Literal runat="server" Text='<%# GetGroupHeading(Eval("Group")) %>' />
代码中的“GetGroupHeading”方法跟踪前一个组标题并返回“
【讨论】:
是的,尼克给了很大的领先优势。这是我的代码隐藏
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>
【讨论】:
试试来自 Rolla 的 4 Guys 的这篇文章:Using ASP.NET 3.5's ListView and DataPager Controls: Grouping Data with the ListView Control
【讨论】: