Decide when to use DataGrid, DataList or Repeater

xiaoxiao2021-03-06  33

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. Introduction to the content of the data web control DataGrid Web Control Analysis DataList In-depth Study Repeater Small Tuning Benchmark Setting Introduction Since there is a simple and script-based Web programming technology like Microsoft Active Server Pages (ASP), Web Development There is a great development. A large boring, repetitive coding work in traditional ASP, does not exist in Microsoft ASP.NET. For example, as all traditional ASP developers have once known, display data in the traditional ASP web page requires the following pseudo code: Create Connection to the DatabasePopulate An ado recordset with a sql querydisplay any Header HTML Neededfor Each Record in The Recordset Print Out The RecordSet Field (s) and associated html move to the next recording HTML MOVE TOTER HTML NEEDED, for example, the content of the record set is displayed in HTML

, the developer has to generate an HTML tag for the
tag (TAG) (Markup), then loop through each record in the record set, generate a tag, and many
tags, and the value of the record set field 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 the data web controls between research data web controls (these differences make them different from other controls), 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 = someDataSourcedataWebControlID.DataBind () Typically, someDataSource DataSource object is assigned attribute data Web control is a DataSet, SqlDataReader, OleDbDataReader or a collection (e.g., Array, ArrayList or System The .Collections Somewhere in the 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 DataGrid Web Control is the most functional in these three data web controls, but when the actual HTML tag generated by the custom control 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? Editcolumn? HyperLinkColumn? TemplateColumn Each column type provides data or provides an interface that allows users and DataGrid to interact. 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 BoundColumn functions 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 rendered 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 Remember that the DataGrid will be presented as HTML

, each DataSource record as a table row (), 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 following itemTemplate will display the field of DataSource CompanyName: <% # databinder.eval (Container.DataItem, "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 in the COMPANYNAME field in a non-bold: <% # DataBinder.eval (Container.DataItem, "CompanyName")%>
<% # dataBinder.eval (Container.DataItem, "ContactName")%> 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 six other templates, including ItemTemplate including a total of seven as follows:? AlternatingItemTemplate EditItemTemplate FooterTemplate HeaderTemplate ItemTemplate SelectedItemTemplate SeparatorTemplate attention, DataGrid's TemplateColumn supports only four templates:?????? ItemTemplate, HeaderTemplate, FooterTemplate and EditItemTemplate. 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, and you must manually add it to the DataList, and the DataGrid BoundColumn column type automatically uses the TextBox web control as editing Interface, and you must explicitly specify the editing interface that you want to edit by edititemtemplate as an editing interface to be edited when using DataList. 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.

Figure 2: The number of requests per second of DataList is as shown in Figure 2, when the DataList is placed within the web form (thus causing the web control to generate it to generate itstate), the web control is much better than DataGrid. Back to top Research 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 HeadeRPlate and FooterTemplate Specifies the HTML tag before and after the data is bound 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, you can use the following REATER syntax:
  • <% # databinder.eval (container.dataitem, "employeename")%> with DataGrid and DataList Different, 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 use bold to display text by setting the control's itemStyle-Font-Bold property to True. . 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. Figure 3: Request number of Repeater Back to top Summary When you display data in the ASP.NET web page, many developers choose their most familiar data web control, 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.

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

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