http://www.yourblog.org/Data/20046/91516.html Author: Scott Mitchell [Overview] WEB Development Ever since the script-based WEB programming techniques (such as ASP), has gone through 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, like most ASP programmers, the process required to display database content in the ASP: Creating a database connection SQL query load ADO dataset display The record output record field value of any HTML code required to traverse data sets and Related HTML Shifts Next Recording Cycle Display The desired HTML code is as follows, in order to display the contents of the record set in the table, we need to output a
tab, and then start cycling. In the loop, each output is output, you need to output a
tag and a number of
tags and TR> tags. Finally, end with a table> 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. [Community of Data Web Controls] It is necessary to see the commonality between them before we discuss the individual features of the three controls. 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, SOMEDASOURCE object refers to data source properties of the data control, which can be DataSet, SqldataReader, OLEDBDataReader or a set of data (such as arrays, array lists) Or other class belonging to the SYSTEM.COLLECTION name space).
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 it 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: Requests of DataGrid's request at the following we have to discuss and test Datalist and Repeater, we will see their performance will be better than DataGrid. [DATALIST Control] As mentioned, 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 data source in the data source: <% # databinder.eval (Container.DataItem, "CompanyName")%> itemtemplate > Asp: datalist> By modifying the above template, we can make the CompanyName field as bold words, and the ContactName field is displayed under normal typical model. <% # databinder.eval (container.DataItem, "CompanyName")%> b> <% # dataBinder . Eval (Container.DataItem ")%> itemtemplate> ask: DATALIST> 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 6 templates: AlternatingItemTemplate · EditItemplate · Footertemplate · HeadeRtemplate · ItemTemplate · SelectedItemTemplate · SeparatorTemplate By default DataList will display records 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 reason for this development time: • Edit / Update / Remove Buttons You can add from the DataCommandColumn in the DataGrid; in Datalist, you need to be added 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: The number of requests for DataList can be seen that in the case of being included by the web form, the performance of DataList is significantly better than DataGrid. [Repeater Control] Repeater control is the most flexible control in 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 Specifies 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:
headertemplate>
<% # Databinder.eval (container.dataitem, "employeename")%> li> itemtemplate> ul> footertemplate> ask: REPEATER> REPEATER class is not inherited from WebControl class This 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" )%> b> li> itemtemplate> If you use DataGrid or DataList, we can only be implemented by setting the itemStyle-font-bold property. 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.