Asp: Repeater - headers when changing a section

is there a way to call a subtitle line when changing a field in asp: repeater control, for example:

Instead

  country |  color |  number
 uk |  red |  3
 uk |  green |  3
 france |  red 3 

Do it:

  == UK ==
 color |  number
 red |  3
 green 3
 == FRANCE ==
 color |  number
 red |  3 

Thanks so much for any help.

+4
source share
5 answers

There seems to be a few bugs in the dotnetjunkies code, but I found a much simpler solution here http://www.west-wind.com/weblog/posts/140753.aspx

Repeaters now do not support grouping, so you need to do it yourself, but it's pretty easy to do. In the repeater above, the following expression corresponds to the grouping:

<%# this.RenderGroup(Eval("Group") as string) %> 

The expression calls a method in the form to render the optional div tag. The code for this simple method is as follows:

 string LastGroup = "@#@~"; protected string RenderGroup(string Group) { if (Group == this.LastGroup) return ""; // *** Group has changed this.LastGroup = Group; return "<div class='groupheader'>" +Group + "</div>"; } 

This code is called for each item, and it just checks to see if the group has changed. If it does not return anything, otherwise a simple div tag is returned as a string to insert into the markup. I decided to send the HTML back, but I suppose you could also return true or false and conditionally display the control in Repeater, which will make the Nazis CSS happier.

0
source

There is no built-in support, but this does not mean that it is impossible.

You need to fire the OnItemDataBound event and have something like this in the markup:

 <asp:Repeater OnItemDataBound="NextItem" ... > <ItemTemplate><asp:Literal Id="Header" Visible="False" Text="{0}<strong>{1}</strong><br/><table>" ... /> <tr> <td><asp:Label id="Color" Text="<%# Eval("Color")" ... /></td> <td><asp:Label id="Number" Text="<%# Eval("Number")" ... /></td> </tr> </ItemTemplate> </asp:Repeater></table> 

Then in the code:

 private string CurCountry = string.Empty; private void NextItem(object sender, RepeaterItemEventARgs e) { if ( e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return; DbDataRecord row = (DbDataRecord)e.Item.DataItem; if (CurCountry != row["country"].ToString() ) { string prev = (CurCounter == string.Empty)?"":"</table>"; CurCountry = row["country"].ToString(); Literal header = (Literal)e.Item.FindControl("Header"); Literal footer = (Literal)e.Item.FindControl("Footer"); header.Text = string.Format(header.Text, prev, CurCountry); header.Visible = true; } } 
+5
source

Another solution is to simply use two repeaters, one of which is embedded in the other. You can transfer your groups with child records to the first repeater and to the ItemDataBound of the group relay, transfer the child records to the child repeater and call DataBind () there.

This is more code, but actually gives you more control over the layout without the HTML code in the code.

As you can see here, we have a parent repeater, and in the element template we can configure each group as we see fit. In ChildRepeater, we have our element template in which we can customize each element within the grouping. Very clean and all with a declarative interface.

 <asp:Repeater runat="server" id="GroupRepeater"> <ItemTemplate> <asp:Literal runat="server" id="HeaderText" /> <asp:Repeater runat="server id="ChildRepeater"> <ItemTemplate> <asp:Literal runat="server" id="InfoGoesHere" /> </ItemTemplate> </asp:Repeater> </ItemTemplate> </asp:Repeater> 

In the code behind, we can have something like this:

 private void GroupRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) { //Get the child records, this can be any data structure you want SomeChildCollection children = ((SomeGroupCollection)e.Item.DataItem).Children; //Find the child repeater Repeater childRepeater = e.Item.FindControl("ChildRepeater") as Repeater; childRepeater.ItemDataBound += SomeMethod; childRepeater.DataSource = children; childRepeater.DataBind(); } 

After attaching each child, you can subscribe to the ItemDataBound event and attach the child to the controls as you like.

+1
source

To execute the solution, I can advise the following: change this line:

 header.Text = string.Format("<strong> {0} </strong><br/><table>", CurCountry); 

to

 header.Text = string.Format(header.Text, CurCountry); 

And how can you customize the look from the .as file? x.

0
source

All Articles