Some discussions about the circumstances in which the DataGrid, DataList, or repeater are used

Source: Internet
Author: User
Tags bind contains html tags requires
DataGrid [Overview]

Web development has undergone a lengthy process since it has scripted web programming technologies such as ASP. Through the use of Microsoft's ASP.net technology, the traditional ASP in a large number of tedious, repetitive programming work has become a history. For example, as most ASP programmers know, the process required to display database content in asp:

Establishing a database connection
Load an ADO DataSet with a SQL query
Display any HTML code that you want
Traversing records in a dataset
Output record field values and associated HTML
Move down one record
Cycle
Display the required HTML code

For example, to display the contents of a recordset in a table, we need to output a <table> tag, and then start the loop. In a loop, each output one record, you need to first output a <tr> tag and a number of <td> tags and </tr> tags. Finally, end with a </table> tag.

One of the main drawbacks of this approach used in traditional ASP is that the HTML code has to be entangled with the ASP source code. For page designers or graphic artists who may not understand programming, it is a disaster to change HTML content. In addition, the amount of code generated is amazing, because we need to make it visible not only from the database, but also from the data.

Fortunately, ASP. NET provides three data controls, making it easy to work with the complexity of the original ASP. These three controls belong to the data Web control, respectively, Datagrid,datalist and repeater. If you know anything about ASP.net database programming, you should at least have the experience of using one of these controls. In most cases, we start with learning to use a DataGrid because it's relatively complete (data display, paging, editing) and relatively simple. However, the DataGrid is not always the right choice.

This article discusses the characteristics of each data control differently from other data controls, as well as the advantages and disadvantages that it brings. Because each data control has its own flaws, there is no perfect choice in programming. You have to weigh the pros and cons of the three controls and decide which one is more appropriate for your program.

To help compare, when discussing each control, we focus on three features: availability (from the page visitor's perspective), development time, and performance. Let's first describe the commonalities of the three controls, and then discuss in depth the characteristics of the three controls, how each control is implemented, and how it reflects availability, development time, and performance.


[The common denominator of data controls (Controls)]

Before we discuss the respective characteristics of the three controls, it is necessary to look at the similarities between them. In general, the most obvious thing about the programming process is that the three controls are used to display data. Another common denominator is the need for a data-bound code to bind the data to the control. This process requires only two lines of code:

Datawebcontrolid.datasource = Somedatasource
Datawebcontrolid.databind ()

Typically, a Somedatasource object is a data source property of a data control, which can be a dataset,sqldatareader,oledbdatareader or a set of data (such as an array, An array list or other class that belongs to a system.collection name space. However, any object that implements the IEnumerable interface can also be bound to a data control.

The DataBind () method iterates through the records in a particular datasource and creates an entry for each record in it and corresponds to the item set of the data control. Each entry in the data control becomes an instance of a class. This class differs depending on the data control. For example, each entry in the DataGrid is an instance of the DataGridItem class, and an entry in repeater is an instance of the RepeaterItem class.

You use different classes to instantiate entries for different data controls because different data controls will display the data and the corresponding HTML code in different ways. For example, the DataGridItem class inherits from the TableRow class, that is, each instance of a DataGridItem can be viewed more or less as a row in a table. This is because the DataGrid is designed to display data in tabular form using the <table> tags in html, so each record is a table row. And repeater is designed to be able to freely define the way data is exported. Therefore, it is not surprising that RepeaterItem does not inherit from TableRow.

The same point for another three data controls is that each control is allowed to use a template to display data. The DataList and Repeater controls must use a template to output data, while the DataGrid allows a column to be displayed by using only a template (described in the Datagrod cell) through TemplateColumn.

Another place that is not quite worth comparing is that both the DataGrid and the DataList inherit from the WebControl class, while repeater inherits from the control class. The WebControl class contains properties for landscaping, such as BackColor, ForeColor, CssClass, BorderStyle, and so on. This means that you can set these properties to personalize the output when using a DataGrid or DataList. Repeater controls do not have these properties. We'll discuss how to use templates to beautify repeater output in the Repeater unit.


[DataGrid control]

Of the three controls, the DataGrid is by far the most powerful, but also the least flexible, control. This lack of flexibility in outputting HTML is because it was originally designed to output data in tabular form. A pair of <tr> tags is created for each record output, and a pair of <td> tags is created for each field's value output.

A DataGrid has several properties that increase its usability. For example, by setting the AllowSorting property of the DataGrid to true and adding a small amount of code, the DataGrid has the ability to sort by a different field. In addition, the ability to set related properties to implement pagination and individual record editing enhances the usability of the DataGrid.

In addition to support for availability, the DataGrid also saves development time. Using the DataGrid to display data on a Web page requires only two lines of code. One row is used to set the data source (DataSource) that is bound to the DataGrid, and the other is used to execute the bind command (DataBind ()). Of course, it's not impossible to implement such a feature in Repeater, but you need to spend a considerable amount of time and effort to implement these features compared to using a DataGrid.

Although the DataGrid has such an impressive advantage, its two shortcomings cannot be ignored. First, as mentioned earlier, the DataGrid has limited functionality in personalized output data. Of course, you can customize the font, color and line width, and so on, but it is always only an HTML table.

Each column in the DataGrid is an instance of the DataGridColumn class. There are five types of DataGrid columns:

· BoundColumn
· ButtonColumn
· Editcolumn
· HyperLinkColumn
· TemplateColumn

Each type will allow page access to interact with the DataGrid in one way. For example, BoundColumn displays DataSource field values as plain text, and HyperLinkColumn displays them as a hyperlink. In addition, developers can customize the DataGrid column style by writing a custom class that inherits from DataGridColumn.

Although the DataGrid has so many properties for enhanced availability, it still seems inflexible and inflexible. This is because, regardless of the attributes, the tables that are generated by the DataGrid need to be set up to take effect. This will undoubtedly make the table bloated and lose flexibility. For example, the DataGridColumn setting takes effect for each row of the table. This limitation of the DataGrid hinders the more creative display of data. For example, if you want every five records to be displayed on one line or do not want a form to display the data at all, you will have to give up using the DataGrid.

The second flaw in the DataGrid is its performance. In three data controls, the DataGrid is the least relativistic. The viewstate generated by the DataGrid will be quite large, especially if the DataGrid contains more rows. Of course, you can also turn off the ViewState feature, but the price is that you won't be able to use sorting, paging, and record editing functions.

To measure the performance of the DataGrid, I used Microsoft's Web application Stress Tool (WAST). The exact test condition settings and the test code will be given at the end of this article.

WAST will send a request to the Web server for a specific URL. Each test will be continuously requested for a URL within one minute. Wast will represent a value for performance, representing how many times the Web server will execute the asp.net page in a second.

Two Tests will display a DataGrid that displays only data. The DataGrid will display the contents of the 4 fields in the Customers table in the Northwinds database (total 91 records). The AutoGenerateColumns property of the DataGrid will be set to true. The first Test places the DataGrid in a form, and the second one is not placed in the form. When you place a control in a form without specifying its enableviewstate to false, the control will always use ViewState to maintain its state. The viewstate is set up to have a time-consuming process and see how it affects page requests per second. The test results are shown in Figure 1.


Figure 1: Number of requests per second to the DataGrid

In the DataList and repeater we are going to discuss and test below, we will see that their performance will be better than the DataGrid.


[DataList control]

As mentioned earlier, the DataGrid uses tables to display data. You may need to further control the display of the data. For example, you want to make the data appear in a table, but not just one record per row, but multiple. Alternatively, you do not want to use tables to display data, but only to display them in a series of <span> tags.

Instead of DataList the concept of the list display data in the DataGrid, it uses a predefined template (Template) to customize the display. By using templates, you can use HTML tags or data binding at the same time. The data binding here is in the form of <%# ...%&gt, which is used to display data records for a given entry in the data source. The following ItemTemplate will display the CompanyName field in the data source:

<asp:datalist runat= "Server" id= "Mydatalist" >
<ItemTemplate&



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.