Some discussions using DataGrid, DataList or Repeater

xiaoxiao2021-03-06  53

Date: Friday, 4 June 2004 Day Weather: Sunny Author: aci

Published: 2004-6-4 17:11:11 Source:

Your blog.com (Yourblog.org)

Author: Scott Mitchell

[Overview]

Web development has experienced a long process since the scripting-based Web programming technology (such as ASP) has experienced a long process. By using Microsoft's ASP.NET technology, there are a large amount of ASP, and the programming of monotonous, repetitive programming work is historical. For example, the process required to display database content in the ASP programmer is known in the ASP programmer:

Establish a database connection

SQL query load ADO data set

Display any HTML code required

Traverse record

Output record field value and related HTML

Move down one record

cycle

Display the required HTML code

Another example, in order to display the contents of the record in the table, we need to output a

tab and then start loop. In the loop, each output is output, you need to output a tag and a number of
tags and tags. Finally, end with a tag.

This method used in traditional ASP has a major disadvantage: HTML code has to be entangled with ASP source code. For those page designers or graphic artists who may not understand the program design, it is undoubtedly a disaster for changing HTML. In addition, the amount of code is also amazing, because we not only need to obtain data from the database, but also need to be visualized.

Ok, ASP.NET provides three data controls that make it easy to work in complicated in ASP. These three controls are data web controls, which are DataGrid, DataList, and Repeater. If you have aware of the ASP.NET database programming, you should at least have experience in using one of the controls. In most cases, we start using DataGrid from learning because it's relatively complete (data display, paging, editing) and relatively simple. However, DataGrid is not always the correct choice.

This article will discuss the characteristics of each data control different from other data controls, respectively, and the advantages and disadvantages of this. It is precisely because each data control has its own defect, so there is no perfect choice in programming. You must weigh the advantages and disadvantages of the three controls and determine which one is more suitable for you.

To help comparison, we will focus on three characteristics: availability (from the perspective of page visits), development time and performance. We first describe the commonality of these three controls, and then discuss the characteristics of three controls, how each control is implemented and how to reflect availability, development time and performance.

[Data Control (Data Web Controls)

Before we discuss the respective features of the three controls, it is necessary to see the common point between them. In general, from the programming process, the most pronounced common point is that these three controls are used to display data. Another common point is a code that requires a data binding code to bind the data to the control. This process only needs two lines of code:

DataWebControlid.DataSource = SomeDataSource

DataWebControlid.database ()

In general, the SomeDataSource object refers to the data source attribute of the data control, which can be DataSet, SqlDataRead, OLEDBDataReader, or a set of data (such as an array, array list, or class that belongs to the System.Collection name). However, objects that implement the IENUMERABLE interface can also be bound to the data control. The database traverses a record in a specific DataSource and establishes an entry and corresponds to the ITEM set of the data control. Each entry in the data control will become an instance of a class. This class is different from the different data controls. For example, each entry in the DataGrid is an instance of the DataGridItem class, and the entry in the Repeater is an instance of the REPEATERITEM class.

The reason why different categories to instantiate different data controls is because different data controls will display data and corresponding HTML code in different ways. For example, the DataGridItem class inherits from the TableRow class, that is, every DataGridItem's instance can be seen as a line in a table. This is because DataGrid is designed to display data in the form of a

tag in HTML, so each record is a table. Repeater is designed to freely define data output. So, RepeaterItem does not inherit it is not surprising from TableRow.

The same point of another three data controls is that each control is allowed to use templates to display data. The DataList and Repeater controls must use templates to output data, while DataGrid allows only templates to display only templates (detailed discussion in DataGrod cells) through TemplateColumn.

Another place that is not very worth more is: DataGrid and DataList are inherited from the WebControl class, while Repeater is inheriting from the Control class. The WebControl class contains some properties for beautification, such as Backcolor, Forecolor, CssClass, BorderStyle, and more. This shows that when using DataGrid or DataList, you can set these properties to personalize output. The Repeater control does not have these properties. We will discuss how to use templates to beautify the output of Repeater in the REATER unit.

[DataGrid Control]

Among the three controls, DataGrid is the most functional, but is also the least flexible control. This feature is not flexible when outputting HTML is because it is originally designed to output data in form. Each of the records will establish a pair of

tags, and a pair of
tags are created when the value of each field is output.

DataGrid contains several properties to improve their availability. For example, by setting the DataGrid's AllowSorting property is True, and add a small number of code, the DataGrid has the functionality that is sorted by different fields. In addition, setting related properties to implement paging and single record editing features more enhanced DataGRID availability.

DataGrid is also quite saving time in addition to support in availability. Use the DataGrid on the web page to display data only two lines of code. One way is used to set the data source (DataSource) bound to the DataGrid, and another is used to execute the bind command (DATABIND ()). Of course, this function is not impossible in the Repeater, just, compare with DataGrid, you need to spend a considerable amount of time and energy to implement these functions.

Although DataGrid has this impressive advantage, it is also impossible to ignore it. First, as mentioned earlier, DataGrid is limited in personalized output data. Of course, you can customize fonts, colors, and line width, but it can only be HTML form. Every column in the DataGrid is an instance of the DataGridColumn class. There are five forms of DataGrid columns:

· BoundColumn

· ButtonColumn

· Editcolumn

· HyperlinkColumn

· TemplateColumn

Each type will allow page access to interact with DataGrid in one way. For example, BoundColumn displays the field value of DataSource as plain text; HyperLinkColumn is displayed as a hyperlink. In addition, developers can customize the style of the DataGrid column by writing a custom class inherited from the DataGridColumn.

Although DataGrid has so many enhanced properties, it still looks dead without flexible. This is because, no matter what the properties need to be effect on the table generated by the DataGrid. This will undoubtedly make the table bloated and lost flexibility. For example, the setting of DataGridColumn takes effect on each row of the table. This limitations of DataGrid hinders more creative display data. For example, you want every five records to be displayed in a row, or do not want to have a form to display data, you will have to give up using DataGrid.

The second defect in DataGrid is its performance. In three data controls, DataGrid is the worst relative performance. ViewState generated by DataGrid will be quite large, especially when DataGrid contains more rows. Of course, you can also close the ViewState feature, but the price is that you will not be able to use sort, pagination, and record editing.

In order to measure the performance of DataGrid, I use Microsoft's Web Application Stress Tool (WAST). Accurate test conditions settings and test code will be given in the end of this article.

WAST will issue a request for a specific URL on the web server. Each test will continue to request a URL within a minute. WAST will represent the value of the value, which will execute the ASP.NET page in a second to the web server.

Two tests will display a DataGrid that only displays data. DataGrid will display the contents of the 4 fields of the Customers table in the Northwinds database (91 records). DataGrid's AutoGenerateColumns property will be set to TRUE. The first test places the DataGrid in an Form, and the second is not placed in the Form. Place the control in Form without specifying its enableViewState to false, the control will always use ViewState to maintain its status. The setting to ViewState is to have a time-consuming process, let's take a look at what is the impact on the page request per second. The test results are shown in Figure 1.

Figure 1: Number of requests for each second of DataGrid

In the DataList and Repeater we have to discuss and tested, we will see their performance will be better than DataGrid.

[DATALIST control]

As mentioned earlier, DataGrid uses a table to display data. You may need to further control the display of data. For example, you want to display data in the table, but not only one record per line, but a number. Alternatively, you don't want to use the form to display data, but only they are displayed in a series of tags.

DataList Abandon the concept of listing of data in DataGrid, but uses pre-defined templates to customize the display. By using a template, you can use the HTML tag or data binding. The data binding here is: <% # ...%>, used to display a data record in the data source. The following itemTemplate will display the companyName field in the data source:

<% # DataBinder.eval (Container.DataItem, "CompanyName")%>

By modifying the above template, we can make the CompanyName field as bold words, and the ContactName field is displayed under normal models in CompanyName.

<% # databinder.eval (container.dataitem, "companyname")%>


<% # Databinder.eval (container.dataitem, "contactname")%>

For each record in the DataList data source, iTemTemplate will display data in the same style by defining an HTML tag. ItemTemplate also supports other six types of other templates:

· AlternatingItemTemplate

· EditItemTemplate

Footertemplate

Headertemplate

· ItemTemplate

· SelectedItemTemplate

· SeparatorTemplate

By default, DataList will display the record in the HTML table. However, by setting the RepeatColumn property, you can set how many records displayed in a row. Further, you can even specify that the content of the DatList is not displayed in the table, but in the tab. This can be implemented by setting the REPEARLAYOUT attribute.

Through templates, RepeatColumn, and RepeatLayout properties, it is clear that DataList is more flexible than DataGrid in custom data output styles, making user interface design more friendly. Of course, we also need to perform functional comparisons, such as paging, sorting, recording editing, and more.

DataList will support record editing by EditIndex Templates and EditCommand, UpdateCommand, and CancelCommand events. However, compare DataGrid, this requires more development time to achieve. There are two main reasons for this development time:

· Editing / Update / Remove Buttons You can add from the DataCOMMandColumn from the DataGrid; in the Datalist, you need to add it manually.

· DataGrid's BoundColumn column style automatically uses text box controls to display record editing interfaces. In DataList, you must explicitly specify what kind of editing interface for use through EditItemTemplate.

Realize the paging in DataList, the sort function is the same as the record editing function, is not very complicated. These features can be implemented through clever programming, just cost some development time. So, if you need a user to sort or edit the data record, you can use DataGrid more convenient than using DataList. DataList's performance is better than DataGrid, especially when DataList is included in the form. Figure 2 shows the test of WAST to DataList.

Figure 2: Number of requests for each second of DataList

It can be seen that in the case of being included by the web form, the performance of DataList is obviously better than DataGrid.

[REPEATER control]

The REPEATER control is the most flexible control in the HTML output in three data controls. Repeater will strictly output data records in accordance with the style you ask. So, if you don't want to form a table mode or simple output data, it is best to use the Repeater.

Like DataList, the Repeater uses templates to specify output styles. REPEATER supports the following five templates:

· AlternatingItemTemplate

Footertemplate

Headertemplate

· ItemTemplate

· SeparatorTemplate

Hedertemplate and FooterTemplate specify the HTML content that should be processed before or after the real record output. AlternatingItemTemplate and ItemTemplate specify the actual HTML style of each output record. For example, you need to bind a DataSet containing employee information to a REPEater, the field name is EMPLOYENAME. If you want to display these records in the page, you can use the following statement:

  • <% # databinder.eval (container.DataItem, "Employeename")%>

    The REPEATER class is not inherited from the WebControl class, which is different from DataGrid and DataList. Therefore, Repeater has no style attributes available for setting. That is, if you want to format the data record of the REPEATER, you must use the HTML tag to set the style. For example, in an example, if we want to display the employee name as a bold, we must set the corresponding HTML tag at ItemTemplate:

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

    And if you use the DataGrid or DataList, we can implement it by setting the itemStyle-font-bold property to TRUE.

    Repeater is lacking on formatting, directly reflected to the development time extension. The more requirements on the output data style, the more the development cycle will be extended. These HTML tags that are specified in the template also become more confused, and it will be more difficult to change in the future page, especially when a new developer takes place. And use DataGrid or DataList you can only set the style properties without using templates. Moreover, if you work with Visual Studio.net or ASP.NET Web Matrix, these properties can be set directly without coding. Because the REPEATER development time is extended, its support on the internal function (paging, sorting, and editing) is insufficient. Therefore, in terms of availability, Repeater has obvious defects. Of course, If users don't matter how to display data, this is not a big problem. The reason why I emphasize this "if" is because, although users are sometimes designed to be able to be able to be paid, sorted or edited, such requirements are often developed later, or after they can see the records they can see. .

    Repeater is the only feature of DataGrid and DataList is its performance, especially better than DataGrid. It is a little higher than DataList.

    [in conclusion]

    When displays data on the ASP.NET page, most of the programmers will use the controls they are familiar with, especially DataGrid. However, such blind choices are unwise to "the best general control". Before selecting controls to display data, you may wish to ask yourself a few questions to help decision: Does users allow users to sort records? Is it a record that needs to be displayed in a non-table? Whether the page will be accessed high frequency, so consider performance?

    DataGrid provides the most features, such as allowing visitors to edit, sort or paginize records. At the same time, it is also the easiest to use, even simply to add it to the page without having to write code. However, these ease of use is at the expense of performance. DataGrid is the lowest efficiency in three controls, especially when using Web Form.

    By using templates, DataList provides more excellent interface effect than DataGrid. However, this needs to be at the expense of certain development times. In order to add sort, paging, and editing, the programmer has to spend more energy than using DataGrid, although its performance is better than DataGrid.

    Finally, Repeater allows the maximum HTML customization for data records. Typically, use Repeater to display data records than to use DataGrid and DataList to take longer development time. In addition, it does not support built-in editing, sorting, and paging features. Fortunately, Repeater is superior to the other two controls in performance, especially better than DataGrid.

    [appendix]

    WAST test setting

    The test is done on a laptop running a Microsoft Windows 2003 Server operating system. Basic configurations are as follows: Intel P4 2.4G CPU; 512 MB RAM; 30GB Ultra ATA Hard Drive; web server uses IIS 6.0; ASP.NET version is 1.1. WAST in this test is set to use single threads, each test time is one minute.

    Test code http://download.microsoft.com/download/9/e/9/9E97B2F8-B317-4751-9AC1-2E34EEBEC26A/DataControlsperftest_setup.msi Click here to download

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

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