ASP.net: using Repeater vs DataList and GridView (DataGrid)

ASP.Net has 3 main Data Web Control: GridView/DataGrid, DataList, Repeater. The Repeater Web control has the greatest flexibility. The Html rendered by GridView and DataList are predefined by the .Net framework, for example some extra table and span tags. But Repeater will be strictly generate the specified Html tags, without addtional html tags.

Just like DataList, Repeater supports the following five templates:

  • AlternatingItemTemplate
  • FooterTemplate
  • HeaderTemplate
  • ItemTemplate
  • SeparatorTemplate

HeaderTemplate and FooterTemplate will be render before and after the main content. AlternatingItemTemplate and ItemTemplate designated for rendering each row records in the datasource. For example: if you want to display employee information with repeater, you can use the code below:

<asp:Repeater runat="server" id="rptEmployees">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><%# Eval("EmployeeName") %></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>

Does like GridView and DataList, Repeater is not derived from WebControl. It does support the Styles attributes. So if you want to style the datas, you have to do it in html tags. for example: if you want to display the EmployeeName in Bold. you have to use "<b>" or css class.

<ItemTemplate>
<li><b><%# Eval("EmployeeName") %></b></li>
</ItemTemplate>
or

<ItemTemplate>
<li class="bold"><%# Eval("EmployeeName") %></li>
</ItemTemplate>
And Repeater does not have build in Paging, Sorting and Edit functions. You have to write some additional functions. But Repeater has a very good advantange: performance. When only displaying data, the repeater is much faster than GridView and DataList. The chart below shows the difference between Repeater, DataList and DataGrid for how many requests the control can handled per second.

Subscribe
Rss Feed Email Follow Us on Twitter
Search