【问题标题】:Which data control would be apt for the scenario?哪种数据控制适合该场景?
【发布时间】:2012-01-15 17:37:49
【问题描述】:

伙计们,我有一份工作来显示图像中给出的订阅者列表。我想知道 ASP.NET 中的哪个数据控件最适合这种情况。我只是在玩 Listview,但我想从这里的人那里得到意见。谢谢。法拉兹。

Update1:​​我应该在这里使用网格视图列表(嵌套在列表视图中)吗?还是可以用一个列表视图来完成?

【问题讨论】:

标签: asp.net listview


【解决方案1】:

对我来说,这是一个带有常用标题文本的 GridView。您可以在网格之前使用任何其他控件或纯 HTML 制作上面的第一个栏。 ListViews 不是网格,我认为不应该在最终预期结果更接近网格时使用

【讨论】:

  • 您好,感谢您的快速回复。但在我的情况下,我必须显示一个网格视图列表(基于订阅者可以拥有的子订阅者数量,这个计数是动态的)。请指教。
【解决方案2】:

显然,grid-view 会为您提供所需的表格布局(使用自动生成的列可能会付出最小的努力)。

但是,如果需要分页、排序、编辑注释,那么我宁愿使用中继器控件。主要原因是对加价的精确控制。例如,grid-view 不支持 <colgroup><thead> 等元素(同样,您的布局可能不需要这些元素)。如果需要分页/排序/编辑等,那么 ListView 是更好的选择。

就显示多个表格而言,您可以使用嵌套控件 - 例如,嵌套网格视图的中继器/列表视图。

编辑:
您不太清楚您拥有的数据结构以及您想要的确切布局。所以这就是我的假设 - 你有一个 List<Subscriber> 包含根订阅者和他们的孩子。在布局中,您需要一张表用于根订阅者,然后是多张表 - 每个根订阅者的孩子一张。

标记将类似于

<asp:Repeater runat="server" ID="Outer" >
  <HeaderTemplate>
     <%-- Put a grid here for parent  -->
     <asp:GridView runat="server" ID="Root" DataSource='<%# GetRootSubscribers() %>' >
         ... column def etc
     </asp:GridView>
  </HeaderTemplate>
  <ItemTemplate>
     <!-- Put a grid here for children for current root subsriber -->
     <asp:GridView runat="server" ID="Child" DataSource='<%# GetChildSubscribers(Eval("MemberID")) %>' >
         ... column def etc
     </asp:GridView>
  </ItemTemplate>
</asp:Repeater>

这将由两种代码隐藏方法支持,例如

protected IEnumerable<Subscriber> GetRootSubscribers()
{
    // I am not sure how you decide if a subscriber is a parent or not, I have just 
    // illustrated a condition where you have a parent id field to indicate the same
    return allSubscribers.Where(s => s.ParentID == null);
}

protected IEnumerable<Subscriber> GetChildSubscribers(object memberId)
{
    // I am not sure how you decide a child subscriber, I have just 
    // illustrated a condition where you have a parent id field to indicate the same
    return allSubscribers.Where(s => s.ParentID.Equals(memberId));
}

// bind the outer repeater to root list
Outer.DataSource = GetRootSubscribers();
Outer.DataBind();

希望这会给您一些关于如何进行的想法。

【讨论】:

  • 嘿,谢谢.. 但在我的情况下,我必须显示一个网格视图列表(基于订阅者可以拥有的子订阅者数量,这个计数是动态的)。请指教
  • @nfa379,如答案中所述,您需要将网格视图嵌套在中继器(或列表视图)中。外部中继器必须绑定到计数与要显示的网格数相同的源。确切的机制取决于您如何获取订阅者数据 - 它是数据表、数据集还是对象图?
  • @nfa379,任何订阅者的子订阅者在哪里?假设您有三个顶级订阅者,每个订阅者有 4、5、6 个子订阅者 - 您应该在此处显示多少个网格?
  • 始终有一个根订阅者,并且可以有 1 个或多个子订阅者。我不必分层显示数据。我必须显示子订阅者的父级和列表(如图所示)。谢谢你的时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-17
  • 1970-01-01
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多