Getting started with DataList controls

Source: Internet
Author: User

DataList control entry: rendering Mode

The DataList control is similar to the DataGrid control. It is also a Web control used in ASP. NET to control data display. To learn about the DataGrid Control, you must first understand that the DataGrid will be rendered as HTML

<Table>

, Each

DataSource

Record as a table row

<Tr>

), Each record field is used as a table column.

<Td>

). Sometimes you may want to control the display of data more. For example, you may want to display data in HTML

<Table>

But not one record per line, but five records per line. Or, you do not want to display the data in

<Table>

To display each element in

<Span>

Marking.

DataList control entry: different from DataGrid

DataList abandons the "column" concept adopted by the DataGrid. On the contrary, the display of DataList is defined by the template. With the template, developers can specify the mixed HTML syntax and Data Binding syntax. The HTML syntax is a standard HTML Tag; the data binding syntax is to use

<% #

And

%>

Tag-separated, used from

DataSource

Records generate content used to construct a given DataList item. For example, the following ItemTemplate will display

DataSource

Field

CompanyName:

 
 
  1. < asp:DataList runat="server" id="myDataList"> 
  2.   < ItemTemplate> 
  3.     < %# DataBinder.Eval(Container.DataItem, "CompanyName") %> 
  4.   < /ItemTemplate> 
  5. < /asp:DataList> 

In addition to the data binding syntax, templates can also contain HTML tags. By updating the template above, you can

CompanyName

Fields are displayed in bold

ContactName

Fields are displayed in non-bold

CompanyName

The fields are as follows:

 
 
  1. < asp:DataList runat="server" id="myDataList"> 
  2.   < ItemTemplate> 
  3.     < b>< %# DataBinder.Eval(Container.DataItem, "CompanyName") %>< /b> 
  4.     < br /> 
  5.     < %# DataBinder.Eval(Container.DataItem, "ContactName") %> 
  6.   < /ItemTemplate> 
  7. < /asp:DataList> 

For each record in DataList DataSource, the Data Binding Syntax of ItemTemplate must be calculated. The output of the Data Binding syntax, together with the HTML Tag, specifies the HTML displayed for the DataList item. DataList also supports six other templates, including ItemTemplate. There are seven templates:

AlternatingItemTemplate

EditItemTemplate

FooterTemplate

HeaderTemplate

ItemTemplate

SelectedItemTemplate

SeparatorTemplate

Note: The TemplateColumn of the DataGrid only supports four templates: ItemTemplate, HeaderTemplate, FooterTemplate, and EditItemTemplate.

By default, DataList displays each item as HTML

<Table>

. However, by setting

RepeatColumns

Attribute. You can specify the number of DataList entries displayed in each row of the table. Besides specifying HTML

<Table>

In addition to the number of DataList entries displayed in each row, you can also specify the content of DataList to use

<Span>

Mark the display instead of using

<Table>

Mark. DataList

RepeatLayout

Attribute can be set to Table or Flow, indicating that data in DataList is displayed in HTML

<Table>

Medium or

<Span>

Marking.

Use templates and

RepeatColumns

And

RepeatLayout

Attribute, it is obvious that DataList allows more customization of the rendered HTML Tag than the DataGrid. This enhanced customization enables the use of DataList to produce more friendly data display, because

DataSource

Record occupies a single HTML of a table row

<Table>

The model cannot always be the best choice for displaying information. However, it is not enough to determine the availability of DataList to study the custom features improved than the DataGrid. We must also compare the sorting, paging, and editing functions of the DataGrid and DataList.

DataList supports inline editing by using the EditItemIndex template and EditCommand, UpdateCommand, and CancelCommand events. However, using DataList to add such a function takes longer development time than using DataGrid. The difference in development time is due to the following two reasons:

The Edit, update, and cancel buttons can be created in the DataGrid using the EditCommandColumn column type. They must be manually added to DataList and

The TextBox Web control is automatically used as the editing interface for the DataGrid BoundColumn column type. When using DataList, you must use EditItemTemplate to explicitly specify the editing interface for the item to be edited.

Although it is not difficult to use DataList for inline editing, it is difficult to sort, paging, and edit DataList data. Although some flexible encoding functions can certainly complete such a function, adding such a function to DataList will take a considerable amount of development time. Therefore, if the end user can sort data and pagination is a required requirement, it is best to select the DataGrid instead of DataList.

DataList control entry: Performance Comparison

The performance of DataList is better than that of DataGrid, which is more obvious when DataList is in a Web form. Figure 2 shows the test results of Web Application Stress Tool on DataList.

 

Figure 2: Number of DataList requests per second

As shown in the result in positive 2, when DataList is placed in a Web form, the Web Control generates its ViewState), the Web control is far better than the DataGrid.

  1. Exploring the operation mechanism of the DataGrid Web Control
  2. Discuss the similarity between ASP. NET data Web controls
  3. Transformation from traditional ASP to ASP. NET: Understanding controls
  4. How to use ASP. NET to debug APIs
  5. Five methods for generating HTML static pages using ASP. NET templates

Related Article

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.