Walkthrough: Displaying, Paging, and Sorting Data Using the ListView Web Server Control

In this walkthrough, you will work with the ListView control, which enables you to display data from a data source in a format that you define by using templates. By working with templates, you can have complete control over the layout and appearance of the data in the control. The ListView control automatically supports edit, insert, and delete operations as well as sorting and paging functionality.

SQL Server Express. By default, this is installed with Visual Studio or Visual Web Developer Express. If you have SQL Server installed, you can use that instead, but you must make small adjustments to some of the procedures.

The AdventureWorks database installed on your computer. For information about how to download and install the SQL Server sample AdventureWorks database, see Installing Sample Databases for Express Editions on the Microsoft SQL Server Web site.

A user name and password for a SQL Server account that has access to the AdventureWorks database.

In the Weblocation box, select File System, and then enter the name of the folder where you want to keep the pages for your Web site.

For example, type the folder name C:\WebSites.

Click OK.

Visual Studio creates a Web site project that includes prebuilt functionality for layout (a master page, the Default.aspx and About.aspx content pages, and a cascading style sheet), for Ajax (client script files), and for authentication (ASP.NET membership).

A connection to a data source such as a database. In the following procedure, you will create a connection to the SQL Server AdventureWorks database.

A data source control on the page, which interacts with the data source (the database) to read and write data. In this walkthrough, you will use a SqlDataSource control that interacts with the SQL Server AdventureWorks database.

A control on the page to display the data. In the following procedure, you will display data in a ListView control, which gets its data from the SqlDataSource control.

To display and page through data in the ListView control

If the Web site does not have an App_Data folder, in Solution Explorer, right-click the project, click Add ASP.NET Folder, and then click App_Data.

In Solution Explorer, right-click the App_Data folder, and then click Add Existing Item.

The Add Existing Item dialog box is displayed.

Enter the location of the AdventureWorks database file (AdventureWorks_Data.mdf).

By default, the .mdf file is installed in the path C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks_Data.mdf.

Note

This procedure will create a copy of the database file in the project. The database file is large. If it is impractical to make a copy of the database, you can connect to it by using an alternative method, such as attaching the database file directly. However, the procedure for doing this is not covered in this walkthrough.

In Solution Explorer, right-click the project and select Add New Item.

You might also want to select a different style, which can make it easier to view the data. To do this, under Select a Style, select a style such as Colorful.

The wizard creates the templates for the ListView control, based on the columns in the query. You can customize the layout by editing the templates that contain the layout elements, controls, and binding expressions.

This walkthrough has shown you the basic steps for using a ListView control to display, page, and sort data records by using one of the layouts provided for the control. You might want to experiment with additional features of the ListView control and explore different scenarios. For example, you might want to edit, delete, and insert records. For more information, see Walkthrough: Modifying Data Using the ListView Web Server Control.