If you look under Data tab on Visual Studio Toolbox, you can see several controls that could be used to display data from some data source, like database or XML file. Standard ASP.NET data presentation controls are:
- DataList,
- DetailsView,
- FormView,
- GridView,
- ListView and
- Repeater.
Every of these controls could be used to display data. Logical question is, why ASP.NET offers so many controls for same task? What are differences between these controls and most important, when to use each of them?
|
In second group are Repeater, DataList, GridView and ListView control. These controls show collection of records, and are commonly called data list controls.
Image 1: Data tab on Visual Studio Toolbox
Repeater control features
Repeater control is derived from Control class. Repeater simply repeats data given in templates. Repeated data is usually HTML code mixed with records from data source. Repeater's output is not predefined. Because of that, Repeater demands most work to define template. In return it gives us most flexibility to build layout and optimize presentation.With same template and same data set, Repeater usually works faster of DataList or GridView controls. This is mostly because of DataReader class, which is used for read only access. DataReader is faster than DataSet or DataTable classes commonly used with GridView.
Compared to GridView and DataList control, Repeater has limited features. By default, Repeater is good for displaying of data. It is not best choice if you need editing of data. Also, by default it doesn't provide paging and sorting of records.
DataList control features
Unlike Repeater, DataList control is derived from WebControl class. That gives it a number of style properties like BorderStyle, BackColor, ForeColor etc.DataList is somewhere between Repeater and GridView, in many ways looks like a compromise between these two. DataList has more features but more limited in design when compared to Repeater. In the other hand, it has more flexibility and less features than GridView.
Also, DataList has RepeatDirection, RepeatColumns and RepeatLayout properties, which are unique for DataList control. These properties are useful when you need to create presentation with more than one record per row, like image gallery, product catalog etc. For example, let say you are creating an image gallery and want to show 5 images per row. Repeater would require checking of record position and manipulating HTML. With DataList, just use RepeatDirection="Horizontal" and RepeatColumns="5".
RepeatLayout could be Table or Flow, depending of do you need HTML table structure in layout.
GridView control features
Like DataList control, GridView is derived from WebControl class and includes numerous style properties. In addition to this, GridView is most complex control and have most properties. Numerous features include data paging, sorting, updating and deleting of records. First versions of ASP.NET had DataGrid control. GridView is replaced DataGrid in ASP.NET 2.0 as improved version of old DataGrid control.GridView displays data in form of grid. Rows and columns are represented as HTML table. This is very useful if you need grid-like presentation. If you need to present data in table layout, then GridView requires a minimum of effort. In same time, if you want more customizable flow layout, GridView is not best option.
Common problem when using GridView control could be large ViewState which makes page loads slower and requires more bandwidth. This could be insignificantly on low traffic website, but keep in mind that for each second while the page loads, the percentage of visitors will become impatient and leave website. Also, massive view state is not good for SEO (search engine optimization).
One more problem is using of default pager on large tables or high traffic websites. If default paging is used, GridView will load complete dataset in memory, which descreases website's performances.
ListView control features
ListView control is newest data presentation control, introduced in ASP.NET 3.5. Previous controls (Repeater, DataList and GridView) logically follow each other. For example, Repater is simplest but fastest, then DataList has more features but more overheads too, and finally GridView is most complex, has most features, but heaviest and thus slowest on page.Now, there is new ListView control which tries to provide best from both sides: speed and flexibility in design, and also a lot of features like paging, updating or deleting of records etc. Because of this, ListView control is often better choice than Repeater or DataList.
Flexibility in design comes with price. To use ListView, you'll probably have to write a lot of markup code. For simple table layout, GridView requires much less work.
ListView has new LayoutTemplate. Instead of Header and Footer templates, ListView has just LayoutTemplate. Main advantage of LayoutTemplate when compared to Header and Footer templates is that LayoutTemplate makes markup code looks more object oriented. So, it is not a big difference between these two models. It is possible to achieve same results with LayoutTemplate on ListView like with HeaderTemplate and FooterTemplate on Repeater, DataList and GridView.
Table comparison of Repeater, DataList, GridView and ListView control
Table comparison is the easiest way to quickly decide which data list control is best for your specific project requirements.
|
Repeater
|
DataList
|
GridView
|
ListView
|
Flow layout
|
Yes
|
Yes
|
No
|
Yes
|
Table layout
|
No
|
No
|
Yes
|
No
|
Style properties
|
No
|
Yes
|
Yes
|
Yes
|
Column layout
|
No
|
Yes
|
No
|
No
|
Paging
|
No
|
No
|
Yes
|
Yes
|
Sorting
|
No
|
No
|
Yes
|
Yes
|
Edit/Delete
|
No
|
No
|
Yes
|
Yes
|
Insert
|
No
|
No
|
No
|
Yes
|
Grouping
|
No
|
Yes
|
No
|
Yes
|
Conclusion
In this tutorial, I covered some differences between ASP.NET data list controls. Repeater, DataList, GridView and ListView have many common traits too. They share properties like DataSource, DataBound, and also events ItemCreated, ItemDataBound etc. Because of similarities between controls, it is usually not hard to switch to another control if first choice is not good. For example, if you used DataList in previous code, you can change it into ListView to use its new InsertTemplate feature.ListView control can exceed the capabilities of Repeater or DataList control, but GridView still has advantage in faster implementation and short markup code. GridView's common problem is large ViewState which could cause page loads slowly.
Default GridView paging opens complete data set in server's memory. For large tables or for high traffic websites, this will overload web server's resources. Even new ListView's DataPager control still opens all records in memory. Also, pages are opened by using JavaScript which means only first page is indexed by search engines. The solution could be to create custom pager, but this takes time needed to create, test and optimize code, as well as later maintenance of separate project.
By recognizing this problem, Bean Software created SEO Pager control which solves all paging issues with Repeater, DataList, GridView and ListView control:
- Supports templates, so your pager look any way you want.
- Application's code works with selected page only, not complete data set. On this way, whatever data list control you choose, it will work fast and web server's resources remains free.
- Supports both standard post back and plain hyperlinks in pager buttons. Search engines like Google follows only plain links and avoid post backs.
- Works with any data list control: Repeater, DataList, GridView or ListView.
ListView (note the edit,group,insert
,layout)
·
AlternatingltemTemplate
·
EditltemTemplate
·
EmptyDataTemplate
·
EmptyltemTemplate
·
GroupSeparatorTemplate
·
GroupTemplate
·
lnsertltemTemplate
·
ItemSeparatorTemplate
·
ItemTemplate
·
LayoutTemplate
·
SelectedltemTemplate
DataList (note the Style pairs)
·
AlternatingltemStyle
·
AlternatingltemTemplate
·
EditltemStyle
·
EditltemTemplate
·
FooterStyle
·
FooterTemplate
·
HeaderStyle
·
HeaderTemplate
·
ItemStyle
·
ItemTemplate
·
SelectedltemStyle
·
SelectedltemTemplate
·
SeparatorStyle
·
SeparatorTemplate
Repeater
·
AlternatingltemTemplate
·
FooterTemplate
·
HeaderTemplate
·
ItemTemplate
·
SeparatorTemplate
Code View (advanced view)
CompositeDataBoundControl:
look the following classes hierarchy
(and related controls).
these controls hosts other asp.net
controls in their templates to display bound-data to user
Some descriptions for better clarifications
The ListView Control
The ListView control also uses templates for the display of
data. However, it supports many additional templates that allow for more
scenarios when working with your data. These templates include theLayoutTemplate,GroupTemplate,ItemSeparatorTemplate.
The ListView control (unlike
DataList and Repeater) also implicitly supports the ability to
edit, insert, and delete data by using a data source control. You can define
individual templates for each of these scenarios.
The DataList Control
The DataList control works like the Repeater control. It repeats data for each row in your data set, and it
displays this data according to your defined template. However, it lays out the data
defined in the template within various HTML structures. This includes options
for horizontal or vertical layout, andit
also allows you to set how the data should
be repeated, as flow or table layout.
The DataList control does not automatically use a data source
control to edit data. Instead, itprovides
command events in which you can write your
own code for these scenarios. To enable these events, you add a Button control
to one of the templates and set the button’s CommandName property to the edit,
delete, update, or cancel keyword. The appropriate event is then raised by the
DataList control.
The Repeater Control
The Repeater control also uses
templates to define custom binding. However, it does not show data as
individual records. Instead, it repeats the data rows as you specify in your
template. This allows you to create a single row of data and have it repeat
across your page.
The Repeater control is a read-only template. That is, it
supports only the ItemTemplate. It does not implicitly support editing,
insertion, and deletion. You should consider one of the other controls if you
need this functionality, otherwise you will have to code this
yourself for the Repeater control.
The above Descriptions are from MCTS
Exam 70-515 Web Applications Development with Microsoft.NET Framework 4 book.
DataGrid is not even mentioned in this
book and is replaced by popular GridViews and answered nicely by other users
No comments:
Post a Comment