Search

Deciding When to Use the DataGrid, DataList or Repeater Part 2

0 views

Understanding DataGrid Rendering and Limitations

The DataGrid is the most familiar data‑display control for many ASP.NET developers. It emits a single HTML <table> element, with each record from the bound DataSource becoming a <tr> row. Each field in the record becomes a <td> cell. This structure works well for tabular data, but it imposes a rigid layout that can feel restrictive when you need to break the data into groups, intersperse images, or switch to a non‑table format such as a list of cards.

One common requirement is to display multiple records in a single table row, effectively creating a grid that shows, say, five companies per line. The DataGrid does not expose a property to control that directly; you would need to write custom HTML or rely on CSS to float table cells. Another scenario is to display each record inside a <span> or <div> so that the visual design mimics a magazine layout. The DataGrid again locks you into the table structure and would require extra wrapper markup that can interfere with styling.

Because the DataGrid’s rendering is predetermined, developers typically use its column types (BoundColumn, TemplateColumn, HyperLinkColumn, etc.) to tailor the display. A BoundColumn is convenient when the data is a plain text string or number, but if you want to embed an image or apply a custom CSS class to a particular column, you must switch to a TemplateColumn. Even then, the table skeleton remains unchanged. While the DataGrid provides built‑in support for sorting, paging, and editing, those features come at the cost of additional HTML that the control generates, which can bloat the page and slow down rendering on older browsers or low‑end devices.

In summary, the DataGrid excels when you need a straightforward, feature‑rich table with minimal effort. Its downside is that the layout is less flexible and can become a source of frustration when you try to adapt it to modern responsive designs or non‑table displays.

DataList: Template-Based Customization and Use Cases

The DataList offers a middle ground between the rigid table output of the DataGrid and the free‑form markup of the Repeater. Instead of defining columns, you define templates that describe how each record should appear. The templates allow a mix of static HTML and data‑binding expressions wrapped in <%# … %>. This gives you the freedom to style each item however you like, while still keeping the structure organized.

Here’s a simple ItemTemplate that shows a company name in bold followed by its contact name on a new line:

Prompt
<asp:DataList runat="server" id="dlCompanies"></p> <p> <ItemTemplate></p> <p> <b><%# DataBinder.Eval(Container.DataItem, "CompanyName") %></b><br /></p> <p> <%# DataBinder.Eval(Container.DataItem, "ContactName") %></p> <p> </ItemTemplate></p> <p></asp:DataList>

Because the DataList treats each item as a single element in the output, you can control how many items appear per row by setting the RepeatColumns property. For example, RepeatColumns="5" causes the DataList to render five items in each table row. If you prefer to avoid tables altogether, set RepeatLayout="Flow"; the DataList will then wrap each item in a <span> or <div> as you specify. This flexibility makes the DataList well suited for layouts that require a grid of cards, thumbnails, or custom list items.

The DataList also supports seven templates: AlternatingItemTemplate, EditItemTemplate, FooterTemplate, HeaderTemplate, ItemTemplate, SelectedItemTemplate, and SeparatorTemplate. By providing an AlternatingItemTemplate, you can distinguish every other row with a different background color. The EditItemTemplate gives you inline editing controls, and the SeparatorTemplate can be used to insert a custom divider between items.

When you need inline editing, the DataList handles the EditItemIndex and raises EditCommand, UpdateCommand, and CancelCommand events. The control, however, does not supply the editing controls automatically. You must write the EditItemTemplate to include <asp:TextBox> elements or other input controls. In contrast, the DataGrid’s EditColumn automatically renders a <asp:TextBox> for each bound field. That difference means that building an editable DataList is more labor intensive, especially if you need to add validation or custom logic.

Sorting and paging are possible in the DataList but require manual implementation. Typically you handle the PageIndexChanged event and apply the sort yourself in code‑behind, then rebind the data. The DataGrid provides these features out of the box, which can reduce development time if those capabilities are required.

Because the DataList still generates a <table> by default, it inherits the ViewState overhead that can slow down page loads when used inside a WebForm. When the RepeatLayout is set to Flow, the ViewState remains, but the markup becomes lighter. The trade‑off is that you lose the table’s inherent accessibility features unless you explicitly add appropriate roles and attributes.

In short, the DataList is ideal when you want moderate customization with templated output and are willing to invest a bit more effort for inline editing or paging. It strikes a balance between the DataGrid’s feature set and the Repeater’s minimalism.

Repeater: Full HTML Control and Performance Considerations

The Repeater is the most flexible of the three controls. It emits exactly the HTML you specify in its templates, with no extra table or span wrappers. If you need to output an unordered list, a table with a custom header, or even a complex card layout, the Repeater gives you that freedom. The control’s template collection includes AlternatingItemTemplate, FooterTemplate, HeaderTemplate, ItemTemplate, and SeparatorTemplate - five templates in total.

Consider a scenario where you have a dataset of employee names and you want to display them inside a <ul> list. The Repeater would look like this:

Prompt
<asp:Repeater runat="server" ID="rptEmployees"></p> <p> <HeaderTemplate></p> <p> <ul></p> <p> </HeaderTemplate></p> <p> <li><%# DataBinder.Eval(Container.DataItem, "EmployeeName") %></li></p> <p> <FooterTemplate></p> <p> </ul></p> <p> </FooterTemplate></p> <p></asp:Repeater>

Notice that the Repeater produces no surrounding markup beyond what you provide. This behavior makes the control leaner and faster to render, especially on pages that display large amounts of data. The absence of ViewState also contributes to its performance advantage.

Because the Repeater does not inherit from WebControl, it lacks the styling properties that the DataGrid and DataList expose. If you want a bold font or centered text, you must embed the appropriate CSS classes or inline styles directly into the templates. While this approach gives you full control, it can clutter the markup and make maintenance harder. For example, turning every employee name bold would require changing the <li> tag in the ItemTemplate to <b><%# … %></b> or adding a class="bold" attribute.

Unlike the DataGrid and DataList, the Repeater offers no built‑in support for sorting, paging, or editing. To provide these features, you must implement them yourself: sort the underlying data source, manage the current page index, and rebind the data. That extra work can be justified if the page is read‑only and performance is a critical concern. In most interactive applications, the lack of native paging and sorting will be a deal‑breaker.

Benchmark tests have shown that the Repeater can handle a higher number of requests per second than both the DataGrid and DataList. When placed inside a WebForm, the Repeater’s lack of ViewState and minimal markup result in faster page loads. For instance, a one‑minute stress test on a Windows 2003 Server laptop using IIS 6.0 and ASP.NET 1.1 revealed that the Repeater processed roughly 40 % more requests per second than the DataGrid under identical conditions.

Because the Repeater offers the most raw control, it is often the choice for developers building custom UI components or APIs that need to output exactly formatted HTML without additional overhead. Its learning curve is steeper, but the performance payoff can be significant for large, data‑intensive pages.

Comparing Features: Sorting, Paging, Editing Across Controls

When choosing a data control, you must consider the end‑user requirements: Do you need to allow sorting by column? Is paging essential? Does the user need to edit data inline? Each control handles these scenarios differently.

The DataGrid is the most feature‑rich out of the box. It supports automatic sorting when you set AllowSorting="true" and paging via AllowPaging="true" and a PageSize. Inline editing is also straightforward thanks to the EditColumn. These capabilities come for free, but the generated markup is bulkier, and the control’s ViewState can increase page size.

The DataList, while lacking automatic sorting and paging, offers a RepeatColumns property that can mimic paging by grouping items visually. However, to enable real server‑side paging, you must manually split the data source and rebind. Sorting likewise must be handled in code‑behind by reordering the data set before binding. The DataList does support inline editing, but you must explicitly create the editing controls in the EditItemTemplate and handle the update logic.

The Repeater offers no built‑in support for these features. If you need sorting or paging, you must sort the data source yourself, then manage the page index and rebind. Editing would require a custom form or separate page. The Repeater’s lightweight nature makes it suitable for read‑only views, but it is ill‑suited for interactive tables unless you invest significant effort in custom code.

In terms of development time, the DataGrid wins when the features are required. The DataList is next, requiring moderate effort to add sorting, paging, and editing. The Repeater demands the most time to add any of these features but offers the greatest control over output.

Performance is another factor. Because the DataGrid emits a full table with ViewState, it can become slow on pages that display many rows. The DataList’s table output is lighter but still incurs ViewState. The Repeater’s minimal markup and absence of ViewState give it a speed edge, especially for large data sets or high‑traffic scenarios.

Ultimately, the choice depends on the balance between feature set, customization, development effort, and performance needs.

Benchmark Settings and Practical Implications

The performance figures presented earlier come from a controlled environment: a Windows 2003 Server laptop equipped with a 2.4 GHz Intel Pentium 4 processor, 512 MB RAM, and a 30 GB Ultra ATA drive. The test harness used IIS 6.0 with ASP.NET 1.1, running the Web Application Stress Tool on a single thread for one minute with no warm‑up or cool‑down phases. Each control was bound to the same data source - a modest dataset of 500 records - to keep the comparisons fair.

In these tests, the Repeater consistently outperformed the DataList, which in turn outperformed the DataGrid in terms of requests per second. The differences were more pronounced when the controls were placed inside WebForms, where ViewState overhead was highest. If your application serves thousands of users who primarily view data rather than edit it, opting for a Repeater or DataList could reduce server load and improve response times.

However, if your users expect interactive features like sorting, paging, or inline editing, the DataGrid’s built‑in functionality may outweigh its performance penalty. In practice, many developers combine controls: a DataGrid for an admin interface with heavy CRUD operations, and a Repeater or DataList for a public-facing page where speed and simplicity are paramount.

For more detailed performance analysis, Scott Guthrie’s ASP.NET Performance Talk from TechEd 2003 offers a deep dive into web control metrics. The talk and accompanying demos are freely available on his website. The discussion covers ViewState size, rendering overhead, and best practices for optimizing data controls. Reading this material can help you fine‑tune your own applications and avoid common pitfalls.

Recommended Resources

  • Scott Guthrie’s ASP.NET Performance Talk (TechEd 2003)
  • Demo code from the talk
  • Understanding the Differences Among the DataGrid, DataList, and Repeater
  • An Extensive Examination of the DataGrid Web Control
  • Data Access and Customization Sample
  • DataGrid, DataList, and Repeater Forums

Suggest a Correction

Found an error or have a suggestion? Let us know and we'll review it.

Share this article

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!

Related Articles