如果我有一个嵌套的 ListView,并且我正在 LinQ 中调用相关表,那么如何对其进行排序,而不诉诸父级的 ItemDataBound 事件?
伪代码(已更新解决方案):
<asp:ListView ID="lv" runat="server" OnItemDataBound="lv_ItemDataBound" >
<LayoutTemplate>
<!-- Product Category Stuff -->
<asp:PlaceHolder Id="itemPlaceholder" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<asp:ListView ID="lvInner" runat="server" DataSource='<%# <%# ((Category)Container.DataItem).Products.OrderBy(p => p.Description) %> %>'>
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>Item Stuff</li>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
也许方法看似简单,但我想要内在的Products按字段排序。我看不到一种以声明方式执行此操作的方法,因为如果我没有记错的话,LinQ 会动态创建此查询,并且不会进行排序。
有什么想法吗?
UPDATE
将示例更新为以下内容:
<%# ((Category)Container.DataItem).Products.OrderBy(p => p.Description) %>
希望它对其他人有帮助!
我的假设是产品是IEnumerable<Product>
(或 IQueryable)。如果是这种情况,为什么不直接将 OrderBy 方法添加到评估中,如下所示:
<%# Eval("Products.OrderBy(p => p.FieldToSortOn)") %>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)