Displaying a message in between a asp.net repeater
-
01-07-2021 - |
Question
I have a dataset which returns a set of data related to some reviews. Some of the data in the dataset were migrated from the another database in to the database I am using. So the dataset contains data of the company A and Company B.
I am using a repeater to display the dataset and I need to display a message in between the repeater saying this is Company A reviews and this is Company B reviews. I don't want to repeat the message. A simple example,
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
Just wonder whether this possible using a single repeater. Or I have to use 2 different repeaters for that and display them separately.
Solution
As far as I know, the ASP.net Repeater does not support grouping. The ideal solution to accomplish what you want (assuming your data is structured in the typical parent-child relationship) would be to use nested repeaters. The outer repeater would be bound to the list of companies that you have, and then the internal repeater would be bound to the reviews for each company.
Here is an example article provided by Microsoft on the typical nested repeater scenario:
OTHER TIPS
You can do that by using extra renders inside the repeater that are render the header when is change. Here is a full example:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%#GetHeader(Container.DataItem)%>
<%#GetData(Container.DataItem)%>
</ItemTemplate>
</asp:Repeater>
and on code behind:
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));
}
and gives this output
data :2
Header :3
data :3
data :4
data :5
data :6
Header :7
data :7
data :8
Ps, To make it work as you like the only thing is that you data must be sort By "Company"
also may help: Want 10 rows in every page while binding to a repeater
Hope this help you to move on.