Decide when to use DataGrid, DataList or Repeater

zhaozj2021-02-16  59

Scott Mitchell

July 2003

Applicable to:

Microsoft ASP.NET

Summary: Understand the three controls of ASP.NET to display data: DataGrid, DataList, and Repeater. Each of these controls has unique features and related advantages and disadvantages. When you create an ASP.NET application that displays data, you choose the correct control for this job. As will be seen in this article, choose to use DataGrid, DataList or Repeater, we must weigh the following three factors: usability, development time and performance. (Page Page Print Page)

Download DatacontrolSperFTest_Setup.msi.

This page

Introduction to similarity between data web controls Research DataGrid Web Control Analysis DataList In-depth Research Repeater Summary Better Settings

Introduction

Since the appearance of simple and script-based web programming technology, WEB development has developed very much. A large boring, repetitive coding work in traditional ASP, does not exist in Microsoft ASP.NET. For example, as all traditional ASP developers know, display data in the traditional ASP web page requires the following pseudo code:

Create connection to the database Populate an ADO Recordset with a SQL query Display any header HTML needed For Each Record in the Recordset Print out the Recordset field (s) and associated HTML Move to the next record Next Display any footer HTML needed

For example, to display the contents of the record set in html

, the developer has to generate an HTML tag (tag) for the
Tag (TAG), and then loop through each record in the recordset, generate one each time tag, and many
tags and values ​​of record set fields to display. Finally, after the cycle, the developer needs to generate the end mark.

This method required by traditional ASP has a big disadvantage: it is closely integrated with the source code of the HTML content and the ASP web page. Because there is no separation code and HTML content, the content of HTML and its difficulties are changed, especially for graphics artists or web designers who do not understand programming technology or web designers. Moreover, since the code is retrieved and the content that generates its content, this integration of code and HTML content is relatively relatively large number of code.

Fortunately, ASP.NET provides three controls that display data in the ASP.NET web page is absolutely simpler than the iterative methods required for traditional ASP. These three controls are DataGrid, DataList, and Repeater, I will be called the data web control later. Perhaps if you have developed an ASP.NET web page, you will have some experience in these three controls. Typically, developers start from learning DataGrid because DataGrid uses simple and it has functions that allow data sorting, paging, and editing. However, when displays data in the ASP.NET web page, DataGrid is not always the best choice for controls.

In this article, we will study the unique characteristics of each control in these data web controls. These features give each data Web control many advantages and disadvantages. Because each data web control has some shortcomings, there is no "perfect" control available for any job. When deciding which controls you use, you must weigh the advantages and disadvantages of each of these three data web controls, and then decide which control is the most appropriate. In order to assist in comparison, we will focus on these three measures: availability (from the angle of web accessers), development time and performance. We first quickly browse the similarity between the three data web controls. Next we will study DataGrid, then study DataList, and finally look at Repeater. For each control, we will study the functions of these controls and discuss how its functional set affects these measurements.

Back to top

Similarity between data web controls

Differences between research data web controls (these differences make them different from other controls), first look at their similarities. From a higher level of view, the most basic similarity is that DataGrid, DataList, and Repeater are designed to perform substantially the same operation: display data. Another similarity to bind data to the code required for the data web control. Specifically, only the following two lines of code:

DataWebControlID.DataSource = SomeDataSource DataWebControlid.Database

Typically, the SOMEDASOURCE object for DataSource properties that assigns a data web control is a DataSet, SqlDataRead, OLEDBDataReader, or a collection (such as Array, ArrayList, or System.Collections namespace). However, any object that implements the IENUMERABLE interface can be bound to the data web control.

The DATABIND () method enumerates the record in the specified DataSource. For each record in DataSource, you will create an item and append to the Items collection of the data web control. Each of the data web controls is a class instance. A particular class for each of the controls depends on the data web control. For example, each of the DataGrid is an instance of the DataGridItem class, and each item in the Repeater is an instance of the REATERITEM class.

Each data web control uses different classes for each of its presence, which determines the HTML tag generated by the data web control. For example, the DataGridItem class is derived from the TableROW class, which means that each DataGridItem is more or less presented as a table line. This makes sense, because DataGrid is designed to display data in the HTML

tag, in HTML
, each item is presented separately. On the other hand, Repeater is designed to allow full customization of its output. Therefore, the REPEATERITEM class is not surprising from the TableRow class.

Another similarity between the data web control is that each control can use the template to provide a highly custom output. DataList and Repeater controls must use templates to specify their content, while DataGrid can choose to use templates for specific columns through the TemplateColumn column type (we will discuss a variety of DataGrid column types in the next section "Research DataGrid Web Control). The last thing that is worth noting is that the DataGrid and DataList controls are derived from the WebControl class, while the Repeater control is derived from the Control class. The WebControl class contains many aesthetics properties such as Backcolor, Forecolor, CssClass, BorderStyle, etc. This means that if you use DataGrid and DataList, you can specify style settings from the properties inherited from the WebControl class. Repeater does not have any such style properties. As we will be discussed in the "In-depth study Repeater" section, any visual settings for the REATER output must be specified in the Template of the Repeater.

Back to top

Research DataGrid Web Control

The DataGrid Web control is the most functional in these three data web controls, but when it is the actual HTML tag generated by the custom control, it is the least flexible. This implantation in the HTML tag is due to the DataGrid is designed to use HTML

to display data in tabular form. Therefore, for each record of each bind to the DataGrid, a separate table row () is created. For each field in the record to be displayed, a separate table is created (
).

DataGrid provides a number of functions that greatly increase the availability of data to be displayed. For example, set the DataGrid's allowsorting property to True and add a source code, the developer can turn a normal DataGrid into a DataGrid that can be sorted by the end user. In addition, add a little workload, developers enhance DataGrid's function to allow data paging or intraline editing of data. These features significantly enhance the availability of DataGrid.

In addition to high availability, DataGrid also provides a short development time. To start using DataGrid to display data in the ASP.NET web page, just add DataGrid to the web page and write two lines necessary code: The first line binds the data to DataGrid's DataSource, the second line calls DataGrid's DataBind () Method. Obviously, as the number of functions added to the DataGrid increases, the development time has also increased, but this is only compared to the development time and other data web controls. Suppose you should allow the data displayed by the REPEater to be sorted. The function of adding such a function is certain, but it requires significant more time and energy than the same operation with DataGrid.

Although DataGrid has good availability and development time score, this control has two inherent disadvantages. First, as mentioned earlier, DataGrid is limited to the function of customizing the presented HTML tag. Yes, you can customize the fonts, colors, and borders of the DataGrid, but the fact is still, when DataGrid displays data, the result will be an HTML

, each record in the DataSource, one of them , each field corresponds to one of the tags. Therefore, even if you can customize the HTML output of each row, the DataGrid is still presented as HTML
. Specifically, each column in DataGrid is a class instance that derives from the DataGridColumn class. There are five built-in DataGrid column types:

BoundColumn? ButtonColumn? Edtcolumn? HyperlinkColumn? TemplateColumn

Each column type provides data or provides an interface that allows users to interact with DataGrid. For example, BoundColumn displays the value of the DataSource field in plain text, and HyperLinkColumn displays a hyperlink, and its text and URL section may be a DataSource field. In addition to these built-in column types, you can also create custom DataGrid column types by creating DataGridColumn classes. (For examples of creating a column for extending the BoundColumn function to restrict the number of display characters, see Creating a Custom DataGridColumn Class.)

With so many DataGrid column types, you may not understand why DataGrid's HTML tags cannot be highly customized. To know, although each DataGrid column type generates different HTMLs during rendering, each column is included in a set of

tags, each line is included in a group of
, where each line is used , each column uses a
. This restriction of DataGrid prohibits more creative data display. For example, if you want to display five records in each table, you can't use DataGrid, you must use DataList or Repeater. In addition, if you want to display data in an HTML tag except , you must unfortunately, you can't use DataGrid.

The second disadvantage of DataGrid is its performance. DataGrid is the worst performance in these three data web controls. Based on this, DataGrid - especially with DataGrids generated by many rows may be very large. If you use DataGrid just to display data, you can turn off ViewState, but you can't do this when using DataGrid sort, paging, or editing.

In order to test the performance of DataGrid, I used Microsoft's free Web Application Stress Tool (WAST). Accurate test conditions and WAST settings are listed in the final "reference setting" section in this article. In addition, the code used can also be downloaded in this article.

This Web Application Stress Tool will issue a specific URL request to the web server. For each test, I have continued to request a URL as quickly as possible within a minute. WAST reports many performance measurement standards; a measure of metrics is the number of requests per second, which indicates how many ASP.NET web pages can be performed per second. For a simple DataGrid that only displays data, two tests have been run. Specifically, DataGrid shows four fields from the Customers table from the Northwinds database (Customers table contains a total of 91 records). The AutoGenerateColumns property of DataGrid is set to TRUE. The first test puts the DataGrid in a web form (
) and the second test is not. If you place a control in the form without explicitly set it to false, then the control keeps its status with ViewState. Creating this viewState item may be a time-consuming process, thus reducing the total number of requests per second, as shown in Figure 1.

As we have to study DataList and Repeater, these two controls provide better performance than DataGrid.

Back to top

Analysis Datalist

I remember that the DataGrid will be presented as HTML

, each DataSource record as a table line (), each record field as a table column (
). Sometimes you might want more to control data display. For example, you may want to display the data in html , but not a record in each row, but a five records per line. Alternatively, you don't want to display the data in the
tag, but want to display each element in a tag.

DataList gives up the "column" concept used by DataGrid. Instead, DataList's display is defined by templates. Using templates, developers can specify a mixed HTML syntax and data binding syntax. HTML syntax is a standard HTML tag; data binding syntax is separated by <% # and%> tag, which is used to generate content for constructing a given DataList item from DataSource records. For example, the itemTemplate below will display the field of DataSource CompanyName:

<% # databinder.eval ("companyName")%>

In addition to the data binding syntax, the template can also contain HTML tags. By updating the above template, the companyName field can be displayed in bold, and the ContactName field is displayed below the COMPANYNAME field in a non-bold:

<% # databinder.eval (container.DataItem, "CompanyName")%>
<% # dataBinder . Eval (Container.DataItem, "ContaTName")%> For each record in DataList's DataSource, you must calculate the data binding syntax of ItemTemplate. The output of the data binding syntax specifies HTML presented for the DataList item with the HTML tag. Datalist also supports other six templates, including the next seven: ItemTemplate.

? AlternatingItemTemplate? Edititemtemplate? Footertemplate? Itemtemplate? SEPARATORTEMPLATE? SEPARATORTEMPLATE? SEPARATORTEMPLATE? SEPARATORTEMPLATE? SEPARATORTEMPLATE? SEPARATORTEMPLATE?

Note that DataGrid's TemplateColumn supports only four templates: iTemTemplate, HeadeRTemplate, FooterTemplate, and EditItemplate.

By default, DataList displays each item as a row in HTML

. However, by setting the REPEATCOLUMNS attribute, you can specify how many DataList items each line of the table. In addition to how many DataList items can specify each line of HTML
, you can specify the content of DataList should be displayed using the tag, not using the
tag. The DataList's REATLAYOUT attribute can be set to Table or Flow, indicating that the data in the DATALIST is rendered in the tag in HTML
.

Using templates and the REPEATCOLUMNS and REPEATLAYOUT attributes, it is clear that the DataList is more customized than DataGrid allows for more custom HTML tags. This enhanced customization makes it possible to produce more friendly data using DataList, because DataGrid's "Each DataSource Record takes up a table line Single HTML

" model is not always used to display information select. However, only the customization of DataGrid is not sufficient to determine the availability of DataList; we must also compare the sorting, paging, and editing of DataGrid and DataList.

With the EditIndex template and EditCommand, UpdateCommand, and CancelCommand events, DataList can support inline editing. However, adding such a function with DataList is longer than the development time spent on DataGrid. The difference in development time is due to the following two reasons:

• You can add the editing / update / cancel button created in the DataGrid through the EditCommandColumn column type. Edited an item explicitly specified editing interface. Although inline editing is not difficult with DataList, DataList's data sorting, paging, and editing are difficult. Although some flexible coding will definitely complete such functions, it will cost a considerable development time to add such functions to DataList. Therefore, if the end user can sort and paginate the data, it is best to choose DataGrid without selecting DataList.

DataList's performance is better than DataGrid performance, which is more obvious when DataList is located in a web form. Figure 2 shows the test results of the Web Application Stress Tool on the DataList.

As shown in Figure 2, when DataList is placed in a web form (thus causing the web control to generate it, the web control is much better than DataGrid.

Back to top

In-depth study Repeater

In all three data web controls, the Repeater Web control provides maximum flexibility in the HTML presenting. DataGrid or DataList automatically contains the contents specified by developers in the preset HTML tag. Different from them, Repeater will strictly generate the specified HTML tag when presenting. Therefore, if you do not want to display data with HTML

or a series of tags, you want to display data in other ways, you must use the REPEATER control.

Just like DataList, use the Repeater to specify the tag. Repeater contains the following five templates:

? AlternatingItemTemplate? FooterTemplate? HeadeRTemplate? ItemTemplate? SeparatorTemplate

HeadeRTemplate and Footertemplate specify that the HTML tag before and after the data binds to the Repeater. AlternatingItemTemplate and ItemTemplate specify the HTML tag and data binding syntax for rendering the DataSource in the DataSource of the Repeater. For example, suppose you want to bind a dataset containing the employee information to the REPEater, one of the data sets is EMPLOYEENAME. If you want to display the employee list in the form of a unordered list on the web page, you can use the following REATER syntax:

Unlike DataGrid and DataList, the Repeater class is not derived from the WebControl class. Therefore, Repeater lacks the style properties of DataGrid and DataList. This is nothing more than saying that if you want to format the data displayed in the REATERER, you must do this in the HTML tag. For example, in the above example, if you want to display the name of the employee with a bold, you must change the itemTemplate to include the HTML bold mark, just like this:

  • <% # databinder.eval ( Container.DataItem, "EMPLOYEENAME")%>

    However, for DataGrid or DataList, you can display text with bold by setting the ItemStyle-Font-Bold property of the control.

    Repeater lacks style properties to greatly increase development time indicators. For example, suppose to determine using the REPEATER display data, these data need to be displayed in a bold, intermediate, and a particular font display with a specific background color. All of this must be specified in several HTML tags, which will soon make the TPEATER template messy. This mess will make it difficult to change the appearance, especially when other people operate the project, they have to view a large number of HTML syntax. This is compared to the DataGrid or DataList specified format. For any of these two controls, you can free the template from messy by specifying the style properties of DataGrid or DataList. In addition, you can use some tools to automatically set the style properties of DataGrid and DataList, such as Microsoft Visual Studio .NET or ASP.NET Web Matrix.

    In addition to extending development time, Repeater has lacking built-in features that help support paging, editing, or data editing. Due to lack of these functional support, Repeater scored very low in availability. Of course, if all you are interested in displaying data, do not have any chic ringtone or whistle, then the REPEATER is not a major shortcomings. I emphasized the word "If" is because, once the web application has been deployed, users will find that they need additional features, such as sorting, paging, and editing.

    Repeater has a compensatory quality (this is not surprising), that is, performance. Repeater's performance is slightly better than DataList, much better than DataGrid performance. Figure 3 shows the number of requests that the Repeater can process each second and compare with the DataGrid and DataList.

    summary

    When displaying data in the ASP.NET web page, many developers choose the data web control they are most familiar with, usually DataGrid. But this blind decision is not sensible because there is no universal "best" data web control. When you decide Which data web control is used for a given web page, you should first consider yourself below to determine which control is best suited for your hand. Do you want to allow users to sort data? Need to display data with non-HTML

  • ? Will the page are accessed a lot, what is the performance of a key issue?

    Because DataGrid allows end users to sort, pags, and edit its data, the DataGrid Web controls in these three data web controls provide the best features set. Because just use DataGrid to add it to the web page and write a few lines of code, DataGrid is also the easiest and easy-to-use data web control. However, easy to use and powerful features are to pay the price, such as performance: DataGrid is the low efficiency in three data web controls, especially when placing it in a web form. By using templates, DataList provides more control over DataGrid on displayed data. However, using the template usually requires more development time DataList to support the inline editing of data than using the column type using the DataGrid, but the workload required to achieve more than DataGrid. Unfortunately, providing paging and sorting support in DataList is not a simple matter. DataList provides better performance than DataGrid to compensate for these lack of built-in functions.

    Finally, the REATER control allows full and comprehensive control of the presented HTML tag. For Repeater, the generated unique HTML is the value of the data binding statement in the template and the HTML tag specified in the template without generating an "additional" HTML like DataGrid and DataList. Since the developer is required to specify a completely generated HTML tag, it usually needs the longest development time required by Repeater. Moreover, Repeater does not provide built-in editing, sorting or paging support. However, Repeater's performance is indeed the best in these three data web controls. Its performance can be compared to DataList, but it is better than DataGrid.

    I wish you all a happy program!

    Back to top

    Benchmark settings

    This test is performed on the Microsoft Windows 2003 Server laptop, which has a 2.4 GHz Intel Pentium 4 processor, 512 MB memory and 30GB Ultra ATA hard drive. The web server used is IIS 6.0; use the ASP.NET version 1.1. In the test, the Web Application Stress Tool is configured to use single-threaded and no warm-up or cooling requests. Each test has been running for a minute.

    转载请注明原文地址:https://www.9cbs.com/read-16383.html

    New Post(0)
    CopyRight © 2020 All Rights Reserved
    Processed: 0.045, SQL: 9