Some discussions about how to use DataGrid, datalist, or Repeater

Source: Internet
Author: User
By Scott Mitchell

[Overview]

Web development has gone through a long process since it comes with script-based Web programming technology (such as ASP. By using Microsoft's Asp.net technology, a large number of tedious and repetitive programming tasks in traditional ASP have become history. For exampleProgramThe process required to display the database content in ASP as known to members:

Establish Database Connection
Use SQLQueryLoad ADO Dataset
Display any HTMLCode
Traverse records in a dataset
Output record Field Values and related html
Move to the next record
Loop
Display the required HTML code

For example, to display the record set content in a table, we need to output a <Table> label and start the loop. Each time a record is output in a loop, A <tr> tag and several <TD> and </tr> tags must be output first. End with a </table> label.

The traditional ASP method has one major drawback: HTML code and ASPSource codeYou have to get together. For page designers or graphic artists who may not understand programming, changing HTML content is undoubtedly a disaster. In addition, the amount of code generated is amazing, because we not only need to retrieve data from the database, but also need to visualize it.

Fortunately, ASP. NET provides three data controls to simplify the complicated work in ASP. These three controls are Data Web controls, including DataGrid, datalist, and repeater. If you know something about ASP. NET database programming, you should have at least experience using one of the controls. In most cases, we start from learning to use the DataGrid because its functions are relatively complete (data display, paging, and editing) and relatively simple. However, the DataGrid is not always correct.Select.

This article will discuss the characteristics of each data control that is different from other data control, as well as the advantages and disadvantages. Because each data control has its own defects, 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.

To help the comparison, we will focus on three features: availability (from the perspective of page visitors), development time, and performance when discussing each control. First, we will describe the commonality of the three controls, and then discuss in depth the characteristics of the three controls, how each control is implemented, and how to reflect availability, development time, and performance.

[Data Web controls]

Before discussing the features of the three controls, it is necessary to take a look at the commonalities between them. In general, from the perspective of the programming process, the most significant thing in common is that these three controls are used to display data. Another thing in common is that a data binding code is required to bind data to the control. This process requires only two lines of code:

Datawebcontrolid. datasource = somedatasource
Datawebcontrolid. databind ()

Generally, the somedatasource object is the data source attribute of the index data control. It can be dataset, sqldatareader, oledbdatareader, or a group of data (such as an array, array list, or other data belonging to the system. collection namespace class ). However, any object that implements the ienumerable interface can also be bound to a data control.

The databind () method traverses records in a specific datasource, creates an entry for each record, and corresponds to the item set of the Data Control. Each entry in the data control becomes an instance of a class. This class varies with data controls. 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.

Different classes are used to instantiate entries of different data controls because different data controls use different methods to display data and corresponding HTML code. For example, the datagriditem class inherits from the tablerow class. That is to say, an instance of each datagriditem can be viewed as a row in a table more or less. This is because the DataGrid is designed to display data in a table using the <Table> label in HTML. Therefore, each record is a table row. The repeater is designed to define data output freely. Therefore, it is not surprising that repeateritem does not inherit from tablerow.

The similarities of the other three data controls are that each control is allowed to use a template to display data. The datalist and repeater controls must use templates to output data, while the DataGrid allows the use of templatecolumn instead of the template to display a column (described in detail in the worker rod unit ).

Another area that is not worth comparing is that DataGrid and datalist both inherit from the webcontrol class and repeater inherit from the control class. The webcontrol class contains some attributes for beautification, such as backcolor, forecolor, cssclass, and borderstyle. This means that when using the DataGrid or datalist, you can set these attributes for personalized output. The Repeater control does not have these attributes. We will discuss in the repeater unit How to Use templates to beautify the repeater output.

[DataGrid Control]

Among the three controls, DataGrid is by far the most functional, but also the most inflexible control. This is not flexible enough to output HTML because it was initially designed to output data in the form of tables. A <tr> tag is created for each record output, and a <TD> tag is created for each field value output.

The DataGrid contains several attributes to improve its availability. For example, by setting the allowsorting attribute of the DataGrid to true and adding a small amount of code, the DataGrid can be sorted by different fields. In addition, the function of setting relevant attributes to implement paging and single record editing enhances the availability of the DataGrid.

In addition to availability support, the DataGrid also saves development time. Only two lines of code are required to use the DataGrid to display data on the web page. One row is used to set the data source bound to the DataGrid, and the other is used to run the binding command (databind ()). Of course, it is not impossible to implement such a function in repeater. However, compared with using DataGrid, you need to spend a lot of time and effort to implement these functions.

Although DataGrid has such impressive advantages, its two shortcomings cannot be ignored. First, as described above, the DataGrid has limited functionality in personalized data output. Of course, you can customize the font, color, and line width, but it can only be an HTML table.

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

· Boundcolumn
· Buttoncolumn
· Editcolumn
· Hyperlinkcolumn
· Templatecolumn

Each type allows page access to interact with the DataGrid in one way. For example, boundcolumn shows the field value of datasource as plain text, while hyperlinkcolumn shows it as a hyperlink. In addition, developers can customize the style of the DataGrid column by writing a custom class inherited from the datagridcolumn.

Although the DataGrid has so many attributes to enhance availability, it still seems rigid and inflexible. This is because no matter what the attribute is, you need to set the table generated by the DataGrid to take effect. This will undoubtedly make the table bloated and lose flexibility. For example, the datagridcolumn setting takes effect for the corresponding columns in each row of the table. This limitation of the DataGrid hinders more creative data display. For example, if you want to display each five records in one row or you do not want a table to display data, you will have to discard the DataGrid.

The second defect of DataGrid is its performance. Among the three data controls, the DataGrid has the worst relative performance. The viewstate generated by the DataGrid is very large, especially when the DataGrid contains many rows. Of course, you can also disable the viewstate function, but the cost is that you will not be able to use sorting, paging, record editing, and other functions.

To measure the DataGrid performance, I used Microsoft's web application stress tool (wast ). The precise test condition setting and test code will be given at the end of this article.

Wast will send a request to a specific URL to the web server. Each test will continuously request a URL within one minute. Wast represents the number of times the Web server executes ASP. NET pages within one second.

Two tests will display a DataGrid that only displays data. The DataGrid displays the content of the four fields in the MERs table in the northwinds database (a total of 91 records ). The autogeneratecolumns attribute of the DataGrid is set to true. In the first test, the DataGrid is placed in a form, and the second is not placed in form. Place the control in form without setting its enableviewstate to false, the control will always use viewstate to maintain its State. Viewstate is set to have a time-consuming processing process. Let's take a look at the effect of viewstate on page requests per second. The test results are shown in Figure 1.

Attachment:Your User Group cannot download or view attachments.
Figure 1: Number of requests to the DataGrid per second

In the datalist and repeater discussed and tested below, we will see that their performance will be better than the DataGrid.

[Datalist control]

As described above, the DataGrid uses tables to display data. You may need to further control the data display. For example, if you want to display data in a table, but there is not only one record per line, but multiple records. Alternatively, you do not want to use tables to display data, but only display them in a series of <span> labels.

Datalist abandons the list display data concept in the DataGrid. Instead, it uses a predefined template to customize the display. By using templates, you can use HTML tags or data binding at the same time. The data binding format here is: <% #... %>, Used to display the data records of 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>
<% # Databinder. eval (container. dataitem, "companyName") %>
</Itemtemplate>
</ASP: datalist>

By modifying the template above, we can display the companyName field as a bold character, while the contactname field is displayed under companyName in the normal format.

<Asp: datalist runat = "server" id = "mydatalist">
<Itemtemplate>
<B> <% # databinder. eval (container. dataitem, "companyName") %> </B>
<Br/>
<% # Databinder. eval (container. dataitem, "contactname") %>
</Itemtemplate>
</ASP: datalist>

For each record in the datalist data source, itemtemplate displays data in the same style by defining HTML tags. Itemtemplate also supports 6 Other templates:

· Alternatingitemtemplate
· Edititemtemplate
· Footertemplate
· Headertemplate
· Itemtemplate
· Selecteditemtemplate
· Separatortemplate

By default, datalist displays records in HTML tables. However, by setting the repeatcolumn attribute, you can set how many records are displayed in a row. Furthermore, you can even specify that the content of the datlist is not displayed in the table, but in the <span> label. This can be achieved by setting the repearlayout attribute.

By using the template, repeatcolumn, and repeatlayout attributes, it is obvious that datalist is more flexible in customizing data output styles than DataGrid, making the user interface design more friendly. Of course, we also need to make functional comparisons, such as paging, sorting, and record editing.

Datalist supports record editing through edititemindex templates, editcommand, updatecommand, and cancelcommand events. However, compared with the DataGrid, this requires more development time. There are two main reasons for this inconsistency in development time:

· The edit, update, and delete buttons can be automatically added in the DataGrid by setting editcommandcolumn. You must manually add the buttons in datalist.

· The boundcolumn Column Style of the DataGrid automatically uses the text box control to display the record editing interface. In datalist, you must use edititemtemplate to specify the editing interface.

Implementing the paging and sorting functions in datalist is the same as the record editing functions and is not very complex. These functions can be implemented through clever programming, which only takes some development time. Therefore, if you need to sort or edit data records, it is much easier to use the DataGrid than to use datalist.

Datalist has better performance than DataGrid, especially when datalist is included in form. Figure 2 shows the wast test on datalist.

Attachment:Your User Group cannot download or view attachments.

Figure 2: Number of requests to datalist per second

It can be seen that the performance of datalist is significantly better than that of DataGrid when it is included by web form.

[Repeater control]

The Repeater control is the most flexible control in HTML output among the three data controls. Repeater strictly outputs data records according to the style you require. Therefore, if you do not want to output data in a table or simply <span>, you 'd better use repeater.

Like datalist, repeater uses a template to specify the output style. Repeater supports the following five templates:

· Alternatingitemtemplate
· Footertemplate
· Headertemplate
· Itemtemplate
· Separatortemplate

Hedertemplate and footertemplate specify the HTML content to be processed before or after the real record output. Alternatingitemtemplate and itemtemplate specify the HTML style of each output record. For example, you need to bind a dataset containing employee information to a repeater. The field name is employeename. If you want to display these records unordered on the page, you can use the following statement:

<Asp: repeater runat = "server" id = "rptemployees">
<Headertemplate>
<Ul>
</Headertemplate>
<Itemtemplate>
<Li> <% # databinder. eval (container. dataitem, "employeename") %> </LI>
</Itemtemplate>
<Footertemplate>
</Ul>
</Footertemplate>
</ASP: repeater>

The repeater class does not inherit from the webcontrol class, which is different from the DataGrid and datalist. Therefore, repeater has no style attributes to set. That is to say, if you want to format and output repeater data records, you must use HTML tags to set the style. For example, if we want to display the employee name in bold, we must set the corresponding HTML Tag in itemtemplate:

<Itemtemplate>
<Li> <B> <% # databinder. eval (container. dataitem, "employeename") %> </B> </LI>
</Itemtemplate>

If you use a DataGrid or datalist, you can only set the itemstyle-font-bold attribute to true.

The lack of repeater in formatting settings directly reflects the extended development time. The more requirements on the output data style, the longer the development cycle. These HTML tags defined in the template become increasingly messy, and it will be more difficult to change pages in the future, especially when a new developer takes over the work. With DataGrid or datalist, you can set only the style attributes instead of the template. In addition, if Visual Studio. NET or ASP. NET web matrix is used, these attributes can be directly set without coding.

Due to the extended development time of repeater, the internal creation function (paging, sorting, and editing) is also insufficient. Therefore, repeater has obvious defects in availability. Of course, <B> If </B> users do not have to worry about how to display data, this is not a big problem. I emphasize this "if" because, although users sometimes do not require paging, sorting, or editing records during design, such requirements are often post-development, or after they can see the displayed records.

The only advantage of repeater over DataGrid and datalist is its performance, especially better than DataGrid. It is slightly higher than datalist.

[Conclusion]

When displaying data on the ASP. NET page, most programmers choose the control they are familiar with, especially the DataGrid. However, it is unwise to blindly choose without the "best universal control. Before selecting a control to display data, you may want to ask yourself a few questions to help you make a decision: Do you want to allow users to sort records? Is the record to be displayed in a non-Table scenario? Is the page frequently accessed, so performance should be considered more?

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 is the most efficient 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.

[Appendix]

Wast test settings

the test is performed in a notebook running the Microsoft Windows 2003 Server Operating System. Computer . The basic configuration is as follows: Intel P4 2.4g CPU; 512 mb ram; 30 GB ultra ATA Hard Drive; Web page servers use IIS 6.0; ASP. NET version is 1.1. In this test, wast is set to use a single thread. Each test time is one minute.

Code http://download.microsoft.com/download/9/e/9/9e97b2f8-b317-4751-9ac1-2e34eebec26a/DataControlsPerfTest_Setup.msi

Dareonly

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.