ASP.NET DataList control - Working with DataList control in ASP.NET


ASP.NET DataList Control


Book Excerpt: Working with the DataList Control

In this chapter, we will cover the ASP.NET DataList control. We will learn about the following:

>Using the DataList control
>Binding images to a DataList control dynamically
>Displaying data using the DataList control
>Selecting, editing and delete data using this control
>Handling the DataList control events


Test your ASP.NET Skills

ASP.NET (41 questions)
C#.NET (38 questions)
VB.NET (33 questions)
.NET Framework (26 questions)
ADO.NET (10 questions)

ASP.NET DataList Control

This chapter excerpt from ASP.NET Data Presentation Controls Essentials by Joydip Kanjilal, is printed with permission from Packt Publishing, Copyright 2007.

The ASP.NET DataList Control

The DataList control like the Repeater control is a template driven, light weight control, and acts as a container of repeated data items. The templates in this control are used to define the data that it will contain. It is flexible in the sense that you can easily customize the display of one or more records that are displayed in the control. You have a property in the DataList control called RepeatDirection that can be used to customize the layout of the control.

The RepeatDirection property can accept one of two values, that is, Vertical or Horizontal. The RepeatDirection is Vertical by default. However, if you change it to Horizontal, rather than displaying the data as rows and columns, the DataList control will display them as a list of records with the columns in the data rendered displayed as rows.

This comes in handy, especially in situations where you have too many columns in your database table or columns with larger widths of data. As an example, imagine what would happen if there is a fi eld called Address in our Employee table having data of large size and you are displaying the data using a Repeater, a DataGrid, or a GridView control. You will not be able to display columns of such large data sizes with any of these controls as the display would look awkward. This is where the DataList control fits in.

In a sense, you can think the DataList control as a combination of the DataGrid and the Repeater controls. You can use templates with it much as you did with a Repeater control and you can also edit the records displayed in the control, much like the DataGrid control of ASP.NET. The next section compares the features of the three controls that we have mentioned so far, that is, the Repeater, the DataList, and the DataGrid control of ASP.NET.

When the web page is in execution with the data bound to it using the Page_Load event, the data in the DataList control is rendered as DataListItem objects, that is, each item displayed is actually a DataListItem. Similar to the Repeater control, the DataList control does not have Paging and Sorting functionalities built into it.

Using the DataList Control

To use this control, drag and drop the control in the design view of the web form onto a web form from the toolbox.

Refer to the following screenshot, which displays a DataList control on a web form:

Using the DataList Control

The following list outlines the steps that you can follow to add a DataList control in a web page and make it working:

1. Drag and drop a DataList control in the web form from the toolbox.
2. Set the DataSourceID property of the control to the data source that you will use to bind data to the control, that is, 
     you can set this to an SQL Data Source control.
3. Open the .aspx fi le, declare the element and defi ne the fi elds as per your requirements.
4. Use data binding syntax through the Eval() method to display data in these defi ned fi elds of the control.

You can bind data to the DataList control in two different ways, that is, using the DataSourceID and the DataSource properties. You can use the inbuilt features like selecting and updating data when using the DataSourceID property. Note that you need to write custom code for selecting and updating data to any data source that implements the ICollection and IEnumerable data sources. We will discuss more on this later. The next section discusses how you can handle the events in the DataList control.

Write your comment - Share Knowledge and Experience

Discussion Board
to know more about datalist


rohit 11-28-2011 05:58 AM


More links

ASP.NET 2 data control

The data controls supplied with ASP.NET 2.0 play an important role in making this ambitious goal a reality. Data source controls provide a consistent and extensible method for declaratively accessing data from web pages......

Understanding Anonymous Types

Anonymous types defined with var are not VB variants. The var keyword signals the compiler to emit a strong type based on the value of the operator on the right side. Anonymous types can be used to initialize simple types like integers and strings but detract modestly from clarity and add little value..............

ASP.NET Caching Interview questions with answers

Define Caching in ASP.NET | Advantages of Caching | What are the types of Caching in ASP.NET? | Explain in brief each kind of caching in ASP.NET.

ASP.NET Globalization-Localization questions with answers

What is Globalization and Localization in ASP.NET? | What are the Globalization approaches possible in ASP.NET? | Implementing ASP.NET Globalization | Define Resource Files and Satellite Assemblies

ASP.NET Session State Management questions with answers

Define Session, SessionId and Session State in ASP.NET. | What is Session Identifier? | Advantages and disadvantages of using Session State Management. | What are the Session State Modes? | Define each Session State mode supported by ASP.NET.

ASP.NET 2.0 Data Controls

One of the important goals of ASP.NET 2.0 is 70% code reduction. The data controls supplied with ASP.NET 2.0 play an important role in making this ambitious goal a reality. Data source controls provide a consistent and extensible method for declaratively accessing data from web pages..............

ASP.NET 2.0 Security Controls

With ASP.NET 2.0, things have changed for the better. For security-related functionalities, ASP.NET 2.0 introduces a wide range of new controls..............

ASP.NET 2.0 New Controls

In addition to the new controls, ASP.NET 2.0 also provides numerous enhancements to existing controls that make these controls more versatile than ever before in building component-based web pages. For example, the Panel control now has a DefaultButton property that specifies which button should be clicked if the user presses the Enter key while the panel has the focus.........

MVC Design

MVC, which stands for Model View Controller, is a design pattern that helps us achieve the decoupling of data access and business logic from the presentation code , and also gives us the opportunity to unit test the GUI effectively and neatly, without worrying about GUI changes at all..........  

Difference between Server.Transfer and response.Redirect.

Following are the major differences between Server.Transfer and response.Redirect.....

Difference between authentication and authorization.

Authentication is the process of verifying the identity of a user.......


Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring