views:

597

answers:

2

Similar to my other question:

I have a ListView bound to a Dictionary. Then I have a nested ListView for the dictionary's value's integers.

I need to limit the number of items bound to the nested list to something like 5, and show a more button in the template.

I can't find a way to get the more button to work, and to correctly limit the number at the same time. I have it working as one or the other right now.

Any ideas? Thanks!

UPDATE:

The markup looks something like this:

<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>
A: 

The Take method will return the first 5 items in your list, but won't modify the list itself. You can then simply check the number of items in the list to determine if the more button needs to be enabled.

someList.Take(5); //use these items in your ListView
moreButton.Enabled = (someList.Count > 5);
Joseph
But how are you going to do that within a nested ListView?
SkippyFire
@Skippy I think I need more information about your nested ListView in order to help you. When someone selects an item from your first ListView, then you're able to derive the list that's needed for the nested ListView, correct? Or am I misunderstanding?
Joseph
I'm just talking about binding the list, and the nested list at the same time. SO the user will see a list of lists.
SkippyFire
So the user hasn't selected anything yet.
SkippyFire
@Skippy Could you elaborate on what you mean by a list of lists? What is showing in the first ListView, and what's showing in the nested ListView?
Joseph
I added some sample markup to the question.
SkippyFire
I got it, I'll post the answer soon.
SkippyFire
+1  A: 
  1. DataBind the main ListView anyway you want.
  2. DataBind the nested ListView programmatically in the ItemDataBound event for the main ListView

Code:

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();
    }
}
SkippyFire