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 Edition. 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.

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.

Switch to or open the Default.aspx file.

Switch to Design view.

From the Data tab of the Toolbox, drag a ListView control onto the page.

On the Common ListView Tasks menu, in the ChooseData Source drop-down list, click <New data source…>.

The Data Source Configuration wizard is displayed.

Click Database.

This specifies that you want to get data from a database that supports SQL statements, which includes SQL Server and other OLE-DB–compatible databases.

In the Specify an ID for the data source box, a default data source control name, SqlDataSource1, is displayed. You can leave this name.

Click OK.

The Configure Data Source wizard is displayed.

Under Which data connection should your application use to connect to a database?, select AdventureWorks_Data.mdf from the list.

Click Next.

The wizard displays a page where you can specify that you want to store the connection string in the configuration file. Storing the connection string in the configuration file has two advantages:

It can be more secure than storing it in the page.

You can use the same connection string in multiple pages.

Make sure that the Yes, save this connection as check box is selected, and then click Next. You can leave the default connection string name.

The wizard displays a page where you can specify what data you want to retrieve from the database.

Select the Specify a custom SQL statement or stored procedure option.

Note:

Typically, you would use the option Specify columns from a table or view. However, because the AdventureWorks database has schema names, in this walkthrough you will create a custom SQL statement.

Click Next

In the Define Custom Statements or Stored Procedures page, enter the following SQL query, which retrieves contact data from the AdventureWorks database.

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.