Databinding with a Repeater

This is simple stuff, but I wanted to store these code snippets somewhere for easy re-use…

This is how to do databinding with a Repeater control in ASP.NET.

In the ASPX file:

<asp :Repeater runat="server" ID="rptPuffar">
          <h1>< %# GetTitle(Container) %></h1>
          <p>< %# Eval("XYZ") %></p>

In the code-behind file:

protected override void Page_Load(object sender, EventArgs e)
   if (!IsPostBack)
      PageDataCollection puffar = GetChildren(this.PuffContainer);
      rptPuffar.DataSource = puffar;

protected string GetTitle(RepeaterItem item)
   PageData page = (PageData)item.DataItem;
   return page.PageName;

In Page_Load(), we connect the repeater to a datasource (in this case a collection of pages from EPiServer).

GetTitle() is a function that is called by the ItemTemplate in the repeater for extracting the string to include in the page (in this case the name of the page). Eval() can also be used directly in the ASPX file to display the value of a property.

As I said, easy stuff, but now I don’t have to write the code again the next time I need it…

2 thoughts on “Databinding with a Repeater”

  1. If you turn off viewstate (enableviewstate=”false”) you’ll have to databind regardless of postback or not, but for most EPiServer sites saving viewstate is better than having to bind the list twice. The number of postbacks on an EPiServer site is very small compared to ordinary GET requests.

  2. I agree with you there. I do use the repeater’s view state above and only data bind it once. Of course there can be cases when other solutions are better, but this is just the basic example.

    Thanks for your useful comment, though.


Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.