This is a discussion on Difference between DataList and Repeater? within the ASP and ASP.NET Programming forums, part of the Web Development category; Hi, Differentiate DataList and Repeater.......
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi poornima below link will explain you about Difference between DataList and Repeater http://www.discussweb.com/vb-net-pro...-datagrid.html Thanks KirthiKa Last edited by KiruthikaSambandam : 01-24-2008 at 06:12 AM. |
| |||
| Hi Poornima, Check out this link for a brief note about the difference between 3 controls: ASP.NET.4GuysFromRolla.com: Understanding the Differences Among the DataGrid, DataList, and Repeater Hope this helps. |
| |||
| Understanding the Differences Among the DataGrid, DataList, and Repeater Introduction One of the many benefits of ASP.NET over its predecessor, ASP, is ASP.NET's data Web controls. The data Web controls are designed to easily display data. For example, the DataGrid Web control is ideal for displaying data in an HTML <table>. (For more information on the DataGrid Web control be sure to read An Extensive Examination of the DataGrid Web Control.) In addition to the DataGrid Web control, there are two additional data Web controls. All in total, the three ASP.NET data Web controls are:
The answer to that question depends on what it is you are needing to accomplish. In this article we'll examine the merits and demerits of each of these three data Web controls, and look at situations where certain controls have benefits over the others. Additionally, we'll examine not only the different advantages and disadvantages, but what things these three controls have in common. |
| |||
| Examining the Similarities Between the Data Web Controls The main similarity between the DataGrid, DataList, and Repeater Web controls is that all three have a DataSource property and a DataBind() method. In order to bind data to one of these data Web controls, the same technique is used:
In addition to these similarities, the data Web controls each also have the following three events:
|
| |||
| Demystifying these Similarities With an Example If you are thoroughly confused right about now, I don't blame you! The process a data Web control undergoes when being bound to data can be a bit perplexing at first. However, it is oftentimes much easier to comprehend these complexities by looking at an example. Consider the case where we have a DataGrid that contains two BoundColumns. Such a DataGrid declaration might look like: <asp:DataGrid runat="server" id="myDataGrid" AutoGenerateColumns="False"> <Columns> <asp:BoundColumn DataField="Field1" /> <asp:BoundColumn DataField="Field2" /> </Columns> </asp:DataGrid> Now, imagine that we are binding a DataSet to this DataGrid in the Page_Load event handler. To accomplish this, we might use the following code: Sub Page_Load(sender as Object, e as EventArgs) Dim myConnection as New SqlConnection(connString) Const strSQL as String = "SELECT Field1, Field2 FROM TableName" Dim resultsDataSet as New DataSet() Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(strSQL, myConnection) myDataAdapter.Fill(resultsDataSet) myDataGrid.DataSource = resultsDataSet myDataGrid.DataBind() End Sub What happens when the DataGrid's DataBind() method is called? As we discussed earlier, the DataGrid's DataBind() method enumerates the records of the DataSet assigned to its DataSource property (namely, resultsDataSet). Realize that when iterating through the DataSet, in actuality you are iterating through the Rows collection of the DataSet's default DataTable. The Rows collection is comprised of a number of DataRow instances, one for each record returned by the specified SQL query. Now, for each DataRow, first, a new DataGridItem is created. Then, the DataGrid's ItemCreated event fires. Next, the DataRow is assigned to the DataGridItem's DataItem property. Following this, the DataGrid's ItemDataBound event fires. This process is repeated for all of the DataRows. This databinding process is common among the three data Web controls, the only difference being that with a DataList or Repeater a DataListItem or RepeaterItem is created in place of a DataGridItem. |
| |||
| A Brief Overview of the DataGrid Web Control The DataGrid Web control was designed to display data in an HTML <table>. Each row in the DataGrid's DataSource is displayed as a row in the HTML <table>. The DataGrid has an AutoGenerateColumns property, which can be set to either True or False. If AutoGenerateColumns is set to True (the default), each field in the DataSource is displayed as a column in the resulting HTML <table>. If, however, AutoGenerateColumns is set to False, then the developer must explicitly specify what columns should appear. One downside of a DataGrid is its rather "blocky" display. That is, the DataGrid displays each DataSource record as a row in an HTML <table>, and each field as a column in said table. While the DataGrid allows for the developer to customize the output for a particular column through the <asp:TemplateColumn>, it still is restrictive in that each DataSource record occupies precisely one HTML <table> row. (For an example of customizing a DataGrid's column using templates, see Combining Two DataSource Fields Into One DataGrid Column.) Despite its limitations on specifying the display of the DataGrid's data, the DataGrid is the most commonly used data Web control due to its impressive feature list. For example, with minimal code and effort, the DataGrid can provide sorting, paging, and in-place editing of its data. (For more on the DataGrid Web control, as well as thorough coverage of its features, be sure to read An Extensive Examination of the DataGrid Web Control.) |
| |||
| A Brief Overview of the DataList Web Control The DataList Web control is useful for displaying data that can be highly customized in its layout. By default, the DataList displays its data in an HTML <table>. However, unlike the DataGrid, with the DataList you can specify via the RepeatColumns how many DataSource records should appear per HTML <table> row. For example, the following code and live demo illustrates having five DataSource records displayed per HTML <table> row. <asp:DataList runat="server" id="dlPopularFAQs" ... RepeatColumns="5" > <ItemTemplate> <b>Question:</b><br /> <%# DataBinder.Eval(Container.DataItem, "Description") %> <p> <b>Views:</b><br /> <%# DataBinder.Eval(Container.DataItem, "ViewCount", "{0:#,###}") %> </ItemTemplate> </asp:DataList> As the code example above shows, DataLists are comprised of a number of templates. Templates can contain a mix of HTML syntax and databinding expressions, as shown in the above ItemTemplate. Databinding expressions are ones delimited by <%# and %>, and contain code that's executed when the DataListItem's DataBind() method is called. The ItemTemplate specifies the template used for each record in the DataSource. The DataList contains other templates, which are listed below:
|
| |||
| A Brief Overview of the Repeater Control The Repeater control, unlike the DataGrid and DataList, is not derived from the WebControl class, and therefore does not have the stylistic properties that are common to all Web controls. (These stylistic properties that are common to all Web controls include Font, ForeColor, BackColor, BorderStyle, and so on.) The Repeater, like the DataList, only supports templates; however, the Repeater has only a subset of the DataList's template options. Specifically, the following templates can be used with the Repeater:
Therefore, the Repeater is a good data Web control choice if you want to display data in, say, an unordered list. As the following code and live demo show, displaying database data in an unordered list using a Repeater is relatively straightforward. All you have to do is add the <ul> tag to the HeaderTemplate, the </ul> tag to the FooterTemplate, and an <li> tag along with the DataSource field you want to display to the ItemTemplate: <asp:Repeater runat="server" id="rptULforFAQs"> <HeaderTemplate> <ul> </HeaderTemplate> <ItemTemplate> <li><%# DataBinder.Eval(Container.DataItem, "Description")%></li> </ItemTemplate> <FooterTemplate> </ul> </FooterTemplate> </asp:Repeater> The Repeater is a good choice if you need to display your data in a format different than an HTML <table>. Unfortunately, the Repeater does not provide any built-in means for editing, sorting, or paging of data. However, these mechanisms could be added programmatically, but would result in a lot of code and effort. Conclusion This article examined the similarities and differences of the three data Web controls: the DataGrid, DataList, and Repeater. The main similarities between these three controls is in how they iterate through their DataSource, building up a collection of DataWebControlNameItems. Furthermore, the three Web controls all share three events: ItemCreated, ItemDataBound, and ItemCommand. Each of the data Web controls has its own strengths and weaknesses. The DataGrid, for example, is great for quickly and easily displaying database data in an HTML <table>, and allows for advanced features such as paging, sorting, and in-place editing to be added with little to no programming. However, the DataGrid is quite limited in the general format with which the data is presented. The DataList allows for more freedom. For example, in an earlier live demo we saw that using the DataList's RepeatColumns property, multiple DataSource records could be displayed in a single HTML <table> row. Additionally, the DataList's content is specified via templates, which allows for a high degree of customization. The Repeater allows for the utmost flexibility in its output, since the only HTML rendered from a Repeater is the HTML generated in its templates. That is, no additional HTML output is generated, as is the case with both the DataGrid and DataList. The Repeater, however, does not have any built-in support for sorting, paging, or editing of its data. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/asp-asp-net-programming/5089-difference-between-datalist-repeater.html | |||
| Posted By | For | Type | Date |
| DiscussWeb IT Community - Technical Support and Technology Discussions | This thread | Refback | 01-24-2008 04:21 AM |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Which is the best among DataList,Repeater and GridView and Why? | poornima | ASP and ASP.NET Programming | 2 | 03-14-2008 08:44 PM |
| How to implement Custom Paging in DataList? | poornima | ASP and ASP.NET Programming | 0 | 01-24-2008 12:57 AM |
| What is the difference between repeater over datalist and datagrid? | anbuchezhians | VB.NET Programming | 5 | 11-22-2007 04:44 AM |
| How to decide on the design consideration to take a Datagrid, datalist or repeater in | oxygen | ASP and ASP.NET Programming | 1 | 07-26-2007 04:09 AM |
| How to add a TemplateColumn dynamically to Repeater? in .net | kingmaker | C# Programming | 1 | 07-26-2007 01:02 AM |