A couple of days ago while I was busy coding I ran into problem where I needed to nest Repeaters. There were a number of articles I found but they all dealt with nested Repeater controls using a DataSet as the data source but I needed to use a custom collection (List<T>).
Now I had not run into this requirement before as I have only been on ASP.Net 2.0/3.5 for about 6 months as we jumped from ASP.Net 1.1/Visual Studio 2003, after 3 years, right to the Visual Studio 2008 beta.
Legends of rock need not read further
To you more experienced 2.0+ guys this may be a moot point because you could do this with your eyes closed, while drinking an expresso, while also catching up on some Purple and Brown on YouTube. For that I commend you but will continue to address the rest of us mere mortal coders :D.
Why so touchy?
I suppose this issue may hold more importance to me because when I ran into the problem I was on a very tight deadline to produce a working prototype. The possibility that we might lose the client if they couldn’t have something to show at their next board meeting compounded my frustration with this road block. Needless to say, being the rockstar that I am, I got it done inspite of this issue and we are all set to give that demo for them.
The real point
Okay to the real point of this article. I threw together a simple project to illustrate how I resolved this problem and how you can avoid being caught of guard by it in the future.
The view
Here is the code for the nested Repeater controls on the WebForm.
<asp:Repeater ID="TeamRepeater" runat="server" OnItemDataBound="OnTeamRepeaterItemDataBound">
<ItemTemplate>
<h2><%# ((NestedRepeaterSample.HockeyTeam) Container.DataItem).Name %></h2>
<ul>
<asp:Repeater ID="PlayerRepeater" runat="server">
<ItemTemplate>
<li><%# ((NestedRepeaterSample.HockeyPlayer) Container.DataItem).Name %></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</ItemTemplate>
</asp:Repeater>
As you can see I have gone with a hockey theme for this example, being Canadian and all, instead of some boring author and book list.
Some setup tasks
In order to get this show on the road I needed to do a few things.
Firstly I needed to create the two classed mentioned in the view code. Here they are.
public class HockeyTeam {
string name;
List<HockeyPlayer> players;
public HockeyTeam(string name) {
this.name = name;
players = new List<HockeyPlayer>();
}
public List<HockeyPlayer> Players {
get { return players; }
}
public string Name {
get { return name; }
}
}
public class HockeyPlayer {
string firstName;
string lastName;
public HockeyPlayer(string firstName, string lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public string Name {
get { return firstName + " " + lastName; }
}
}
Nothing complicated here.
Now with our HockeyTeam and HockeyPlayer classes created I am going to populate them. In a real situation they would probably be filled from a database.
void CreateCollections() {
//assume we would load this from a database or
//some other data source
teams = new List();
HockeyTeam leafs = new HockeyTeam(”Maple Leafs”);
leafs.Players.Add(new HockeyPlayer(”Mats”, “Sundin”));
leafs.Players.Add(new HockeyPlayer(”Darcy”, “Tucker”));
leafs.Players.Add(new HockeyPlayer(”Bryan”, “McCabe”));
HockeyTeam senators = new HockeyTeam(”Senators”);
senators.Players.Add(new HockeyPlayer(”Daniel”, “Alfredsson”));
senators.Players.Add(new HockeyPlayer(”Jason”, “Spezza”));
senators.Players.Add(new HockeyPlayer(”Wade”, “Redden”));
HockeyTeam penguins = new HockeyTeam(”Penguins”);
penguins.Players.Add(new HockeyPlayer(”Sidney”, “Crosby”));
penguins.Players.Add(new HockeyPlayer(”Marian”, “Hossa”));
penguins.Players.Add(new HockeyPlayer(”Petr”, “Sykora”));
teams.Add(leafs);
teams.Add(senators);
teams.Add(penguins);
}
Now being Canadian I love hockey but for some reason I like the Maple Leafs despite their futile efforts, it seems, year after year. There is some significance to the teams I chose for this example.
Firstly, Toronto is my favorite team so that’s a given. I enjoy nothing more than seeing Toronto spank the Sens until they cry, except maybe Toronto destroying Montreal. My hate for Montreal would not let me include them in this demo.
Finally, the Penguins Sidney Crosby holds a warm spot in my heart since he is from Cole Harbour, Nova Scotia, just over an hour from where I live.
Bringing it home
Next we need to do some work when the TeamRepeater binds an item. This includes identifying the current item being bound and casting it to our HockeyTeam class, locating the PlayerRepeater within the TeamRepeater, and then binding, to the PlayerRepeater, the players from the current team being bound. Here is the code.
protected void OnTeamRepeaterItemDataBound(object sender, RepeaterItemEventArgs e) {
RepeaterItem item = e.Item;
HockeyTeam currentTeam = (HockeyTeam) item.DataItem;
Repeater PlayerRepeater = (Repeater) item.FindControl("PlayerRepeater");
PlayerRepeater.DataSource = currentTeam.Players;
PlayerRepeater.DataBind();
}
So now with everything in place we can look at our Page_Load method.
protected void Page_Load(object sender, EventArgs e) {
CreateCollections();
TeamRepeater.DataSource = teams;
TeamRepeater.DataBind();
}
Again nothing special, just create the collections and bind to the data source.
Conclusion
Well if you stuck around until this point; thanks for enduring my long winded walkthrough. When you fire this up you should see each team listed with a nice bulleted list of players underneath them.
Hopefully this will help someone in some way if for nothing more than pure entertainment and reminiscing about the days when you weren’t a demi-coder and had to worry about menial tasks like this. Cheers!
Did you like this post? Be sure to grab my RSS feed so you don't miss out on more great articles.

Recent Comments