我有一个数据集,它返回一组与一些评论相关的数据。数据集中的一些数据是从中的另一个数据库迁移到我正在使用的数据库中的。因此,数据集包含A公司和B公司的数据。
我正在使用中继器来显示数据集,我需要在中继器之间显示一条消息,说这是a公司的评论,这是B公司的评论。我不想重复这个信息。一个简单的例子,
Company A reviews
Review 1- I am recommending this company, 10 out of 10
Review 2- I am recommending this company, 9 out of 10
Company B reviews
Review 3- I am recommending this company, 10 out of 10
Review 4- I am recommending this company, 9 out of 10
只是想知道这是否可能使用一个中继器。或者我必须使用两个不同的中继器,并分别显示它们。
据我所知,ASP.net中继器不支持分组。实现您想要的目标的理想解决方案(假设您的数据是以典型的父子关系构建的)是使用嵌套中继器。外部中继器将绑定到您拥有的公司列表,然后内部中继器将绑定每个公司的评论。
以下是微软提供的一篇关于典型嵌套中继器场景的示例文章:
http://support.microsoft.com/kb/306154
您可以通过在中继器内部使用额外的渲染来实现这一点,这些渲染在更改时渲染标头。下面是一个完整的例子:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%#GetHeader(Container.DataItem)%>
<%#GetData(Container.DataItem)%>
</ItemTemplate>
</asp:Repeater>
和代码背后:
public int cLastGroup = 1;
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = new List<int> { 2, 3, 4, 5, 6, 7 , 8 };
Repeater1.DataBind();
}
public string GetHeader(object oItem)
{
// some how like that I will made yours
// if(cLastGroup != current) {cLastGroup = current; return string.format()}
// but now for test I make the 3 and 7 as header
if (((int)oItem) == 3 || ((int)oItem) == 7)
return string.Format("<br><br> Header :{0}", (int)oItem);
else
return string.Empty;
}
public string GetData(object oItem)
{
return string.Format("<br> data :{0}", ((int)oItem));
}
并给出该输出
data :2
Header :3
data :3
data :4
data :5
data :6
Header :7
data :7
data :8
Ps,要想让它按你喜欢的方式工作,唯一的事情就是你的数据必须按"公司"分类
也可能有帮助:在绑定到中继器时,希望每页有10行
希望这能帮助你继续前进。