我有一个数据集,它返回一组与一些评论相关的数据。数据集中的一些数据已从另一个数据库迁移到我正在使用的数据库中。因此,数据集包含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
我想知道用一个中继器能否做到这一点。或者我必须使用两个不同的中继器,并分别显示它们。
发布于 2012-09-10 23:47:46
据我所知,ASP.net转发器不支持分组。要实现您想要的内容(假设您的数据是以典型的父子关系构建的),理想的解决方案是使用嵌套中继器。外部中继器将绑定到您拥有的公司列表,而内部中继器将绑定到每个公司的评论。
以下是Microsoft提供的一篇关于典型嵌套中继器场景的示例文章:
http://support.microsoft.com/kb/306154
发布于 2012-09-11 02:27:27
你可以通过在中继器中使用额外的渲染来做到这一点,当头部发生变化时,这些渲染就会渲染出来。下面是一个完整的示例:
<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,为了让它像你喜欢的那样工作,唯一的事情是你的数据必须按“公司”排序
也可能会有帮助:Want 10 rows in every page while binding to a repeater
希望这能帮助你继续前进。
https://stackoverflow.com/questions/12354994
复制相似问题