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…
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.
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.
/Emil