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"> <itemtemplate> <h1>< %# GetTitle(Container) %></h1> <p>< %# Eval("XYZ") %></p> </itemtemplate> </asp>
In the code-behind file:
protected override void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { PageDataCollection puffar = GetChildren(this.PuffContainer); rptPuffar.DataSource = puffar; rptPuffar.DataBind(); } } 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…