【问题标题】:Limit the number of results in a Nested ASP.NET ListView限制嵌套 ASP.NET ListView 中的结果数
【发布时间】:2009-06-19 13:42:26
【问题描述】:

类似于my other question:

我有一个绑定到字典的 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>

【问题讨论】:

    标签: c# asp.net data-binding listview collections


    【解决方案1】:
    1. DataBind 主 ListView 随心所欲。
    2. 在主 ListView 的 ItemDataBound 事件中以编程方式对嵌套的 ListView 进行 DataBind

    代码:

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

    【讨论】:

      【解决方案2】:

      Take 方法将返回列表中的前 5 个项目,但不会修改列表本身。然后,您可以简单地检查列表中的项目数以确定是否需要启用更多按钮。

      someList.Take(5); //use these items in your ListView
      moreButton.Enabled = (someList.Count > 5);
      

      【讨论】:

      • 但是你将如何在嵌套的 ListView 中做到这一点?
      • @Skippy 我想我需要更多关于你的嵌套 ListView 的信息来帮助你。当有人从您的第一个 ListView 中选择一个项目时,您就可以派生嵌套 ListView 所需的列表,对吗?还是我误会了?
      • 我只是说绑定列表,同时绑定嵌套列表。所以用户会看到一个列表列表。
      • 所以用户还没有选择任何东西。
      • @Skippy 您能否详细说明列表列表的含义?第一个 ListView 中显示什么,嵌套 ListView 中显示什么?
      猜你喜欢
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多