The MVC framework: the Repeater control

Today we’re going to look at the last available control on the futures assembly: we’re talking about the Repeater control. If you’ve dealt with web forms server controls in the past, then looking at the code of the Repeater class shouldn’t make you have any “haha moment”. As you might expect, the class expands the MvcControl class and adds a couple of properties to it:

  • ItemTemplate: property of type ITemplate which lets you specify a template that should be applied to all the items;
  • EmptyDataTemplate: lets you sepcify an ITemplate instance that will be rendered when there aren’t any items to render;
  • Name: used for trying to get the collection of items from the view data dictionary that could have been passed to the view.

This class doesn’t really do much in the Render method. It will only try to get an IEnumerable instance from the view data dictionary (by using the Name property as the key) and if it finds data, it will  instantiate its ItemTemplate for each item of the collection. As you might have guessed, when there’s no data, the class will only render the stuff you’ve specified on the EmptyDataTemplate property.

Since we’re looking at the internals, our study wouldn’t be complete without mentioning the RepeaterItem class. This is a simple wrapper class which expands the Control base class and implements the IDataItemContainer and IViewDataContainer interfaces. When you pass a valid collection on the view data dictionary, the Repeater control will instantiate an instance of this type for each existing item on that collection. And that’s all you need to know about the internals. Lets see how we can use this in a web form view…

We’ll start by creating a new collection which is going to be passed to the view. Notice that we can even get away with anonymous objects (which is great for demo code like this):

var collection = new[] {
                                         new {
                                                     Id = 1,
                                                     UserName = "Luis"
                                         new {
                                                     Id = 2,
                                                     UserName = "Paulo"
                                         new {
                                                     Id = 3,
                                                     UserName = "Jose"
ViewData["repeaterItems"] = collection;

Now,we only need to setup the repeater. In this code, we’ll only use some simple MVC labels for showing the output:

<mvc:Repeater runat="server" Name="repeaterItems">
         Id: <mvc:Label runat="server" Name="Id" /><br />
         Name: <mvc:Label runat="server" Name="UserName" /> <br />

And that’s it. If you load the page you should see a list of users passed from the view. Do keep in mind that the “old” binding expressions still work. For instance, you could have used these expressions for getting the same info from the collection:

<%# Eval("Id") %> – <%# Eval("UserName") %>

You can even do more complicated things…For instance, you can render collections of collections, like Eilon shows in this post.

One of the things you will not get in the  current release is the ability to specify an alternating template in markup. For instance, lets say that you wished to specify a different css class for the container div used inside the ItemTemplate of the repeater. You’ll have to write some simple code to make that happen. Here’s an example of how you could do that:

<mvc:Repeater runat="server" Name="repeaterItems" ID="myRepeater">
       <div class="<%# GetCss(Container) %>">

As you can see, we can still use our old good *Container* to get a reference to the RepeaterItem instance that is being used for the rendering. Then, we could prob

ably write code like this to get the correct css:

protected String GetCss(object obj) {
      var item = obj as Microsoft.Web.Mvc.Controls.RepeaterItem;
      return ( item.DataItemIndex%2  )== 0 ? "even" : "odd";

I say *probably* because the truth is that you can’t. The reason is simple: there’s a bug on the rendering logic and the RepeaterItem is always initialized with index 0. It’s simply to solve this: just increment the index counter and recompile the code.

And I believe there’s not much to talk about on the MVC controls available on the future assembly. We should be getting more stuff when the final version of MVC is out, but the truth is that for now there really isn’t much more than can be said about these controls. On the other hand, there are still lots of stuff to talk about on the MVC futures assembly! So, keep tuned if you want to read more about MVC.


~ by Luis Abreu on March 16, 2009.

3 Responses to “The MVC framework: the Repeater control”

  1. Where is the fckin” repeater class itself

  2. I can”t find any repeater class on MVC!!!

    Is that a separate secret download somewhere?

  3. It was on the futures assembly:

    there”s still a download called futures, but I haven”t used preview 2 yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: