我有一个绑定到Dictionary的ListView。然后我有一个嵌套的ListView用于字典值的整数。
我需要将绑定到嵌套列表的项目数量限制为5,并在模板中显示更多按钮。
我找不到让更多按钮工作的方法,并且无法同时正确地限制数字。我现在正在努力工作。
有什么想法吗?谢谢!
更新:
标记看起来像这样:
<asp:ListView runat="server" ID="MainListView" ItemPlaceholderID="PlaceHolder2">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="PlaceHolder2" />
</LayoutTemplate>
<ItemTemplate>
<h1>My Main ListView - <%# Eval("Key") %></h1>
<asp:ListView runat="server" ID="NestedListView" ItemPlaceholderID="PlaceHolder3"
DataSource='<%# Eval("Value") %>' >
<LayoutTemplate>
<h2>One of many Nested ListViews</h2>
<asp:PlaceHolder runat="server" ID="PlaceHolder3" />
</LayoutTemplate>
<ItemTemplate>
<asp:LinkButton runat="server" ID="AnInteger" Text='<%# Eval("value") %>'></asp:LinkButton>
<br />
</ItemTemplate>
</asp:ListView>
<asp:LinkButton runat="server" ID="uxMoreIntegers" Text="More..." Visible="false" OnClick="uxMoreIntegers_Click"></asp:LinkButton>
</ItemTemplate>
</asp:ListView>
答案 0 :(得分:1)
代码:
protected void uxListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem item = (ListViewDataItem)e.Item;
// Get the bound object (KeyValuePair from the dictionary)
KeyValuePair<string, List<int>> nestedIntegerList = (KeyValuePair<string, List<int>>)item.DataItem;
// Get our nested ListView for this Item
ListView nestedListView = (ListView)e.Item.FindControl("uxNestedListView");
// Check the number of items
if (nestedIntegerList.Value.Count > 5)
{
// There are more items than we want to show, so show the "More..." button
LinkButton button = (LinkButton)item.FindControl("uxMore");
button.Visible = true;
}
// Bind the nestedListView to wahtever you want
nestedListView.DataSource = nestedIntegerList.Value.Take(5);
nestedListView.DataBind();
}
}
答案 1 :(得分:0)
Take方法将返回列表中的前5个项目,但不会修改列表本身。然后,您可以简单地检查列表中的项目数,以确定是否需要启用更多按钮。
someList.Take(5); //use these items in your ListView
moreButton.Enabled = (someList.Count > 5);