ASP. NET provides three data controls to simplify complicated work in ASP. These three controls are Data Web controls, including DataGrid, datalist, and repeater. Generally, it starts from the DataGrid (at least I started this way). Each Data Control has its own defects, so there is no perfect choice in programming. You must weigh the advantages and disadvantages of the three controls and decide which one is more suitable for your program.
Commonalities:
1. Are used to display data
2. All need to be bound
Datawebcontrolid. datasource = somedatasource
Datawebcontrolid. databind ()
Somedatasource is generally the data source of the index data control, including dataset and sqldatareader.
Oledbdatareader or a group of data
Databind () traverses the records in a special datasource and creates a corresponding item set for each record. Each record in the Data Control will become an instance. For example, each entry in the DataGrid is an instance of the datagriditem class, while the entry in the repeater is an instance of the repeateritem class.
3. Each control is allowed to display data using a template.
Differences:
The datalist and repeater controls must use templates to output data. The DataGrid allows the use of templatecolumn instead of the template to display only one column of datalist, instead, use the predefined template to customize the display:
<Asp: datalist runat = "server" id = "mydatalist">
<Itemtemplate>
<% # Databinder. eval (container. dataitem, "companyName") %>
</Itemtemplate>
</ASP: datalist>
Itemtemplate also supports 6 Other templates:
· Alternatingitemtemplate · edititemtemplate · footertemplate · headertemplate · itemtemplate · selecteditemtemplate
· Separatortemplate
Datalist supports record editing through edititemindex templates, editcommand, updatecommand, and cancelcommand events. Datalist has better performance than DataGrid, especially when datalist is included in form.
Repeater control
The Repeater control is the most flexible control in HTML output among the three data controls. It uses templates to specify output styles.
Repeater supports the following five templates:
· Alternatingitemtemplate · footertemplate · headertemplate · itemtemplate · separatortemplate
The repeater class does not inherit from the webcontrol class, which is different from the DataGrid and datalist.
The only advantage of repeater over DataGrid and datalist is its performance, especially better than DataGrid.
DataGrid provides the most functions, such as allowing visitors to edit, sort, or pagination records. At the same time, it is also the easiest to use, or even simply add it to the page without writing additional code. However, these usability is at the cost of performance. DataGrid has the lowest efficiency among the three controls, especially when using web form.
By using templates, datalist provides better interface effects than DataGrid. However, this is at the cost of a certain amount of development time. To add sorting, paging, and editing functions, programmers have to spend more time coding than using the DataGrid, although its performance is better than that of the DataGrid.
Finally, repeater allows you to customize data records in HTML to the maximum extent. Generally, using repeater to display data records takes longer development time than using DataGrid and datalist. In addition, it does not support built-in editing, sorting, and paging functions. Fortunately, repeater is superior to the other two controls in terms of performance, especially better than DataGrid.