Some discussions about how to use DataGrid, DataList, or Repeater (1) ambushaa [translation]

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 example, as most ASP programmers know, the process required to display database content in ASP:

Establish Database Connection

Load An ADO dataset using SQL query

Display any HTML code required

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 tag and start the loop. In a loop, each time a record is output, a tag and several pairs of tags need to be output first, and finally ended with a tag.

The traditional ASP method has one major drawback: HTML code and ASP source code have to be entangled. 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, DataGrid is not always the right choice.

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 the form of tables with HTML tags, so 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.

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.