Monday, March 14, 2011

ListView Control in ASP.NET 3.5 – 1

ListView is one of the new data controls that was shipped with the latest release of ASP.NET, which is 3.5. It displays the values from a data source by utilizing user-defined templates. This gives the developer more flexibility about the design of the data displayed on the user interface. In order for the ListView control to display its content, templates should be created for different parts of the control. The LayoutTemplate and ItemTemplate are mandatory. All other templates are optional.
This post will explain some of the useful features and properties of ListView by dividing the content into the meaningful parts:

Some Definitions Before Jumping Into Coding (via MSDN)
LayoutTemplate: The root template that defines a container object (such as a table, div or span elements) that will contain the content defined in the ItemTemplate. It may also contain a DataPager object.
ItemTemplate: Defines the data-bound content to display for individual items (such as rows).
AlternatingItemTemplate: Defines the content to render for alternating items to make it easier to distinguish between consecutive items.
DataPager: Provides paging functionality for data-bound controls that implement the IPageableItemContainer interface, such as the ListView control.
Sorting: (ListView’s Sorting event) Occurs when a Sort button (a button with its CommandName property set to Sort) is clicked or the Sort method is called, but before the ListView control handles the sort operation.
Part 1 – Simple Binding
In this post, VB.NET and AccessDataSource will be used. When you download the source code, you will see the sample Access database in the App_Data folder. It basically contains one table which is named as Country with the fields of CountryID (AutoNumber, Indexed), CountryName (Text, 50) and CapitalCity (Text, 50). This Country table holds some country names and their associated capital city names.
As mentioned before in the Introduction section, LayoutTemplate and ItemTemplate are mandatory templates in order to initialize the data binding of a ListView:

As a result of this code, HTML presentation should be like:

Part 2 – With DataPager
ListView1 is now ready to be added more functionalities such as paging. To provide this, DataPager object will be used and in order to utilize a DataPager within ListView1, following code can added inside the LayoutTemplate, preferably to the end:

As a result of adding this code, HTML presentation should now be like:

Part 3 – With AlternatingItemTemplate
In order to describe the appearance of alternating rows of ListView1, AlternatingItemTemplate property can be used. It is very similar to the same one in Repeater control. Following code can be added after the ItemTemplate:

Result should look like:

Part 4 – With Sorting Capability
ListView control has a built-in sorting capability. This functionality is defined in the CommandArgument property of the button that contains the columns to be sorted. It occurs when a button with its CommandName property set to Sort is clicked or the Sort method is called. First approach will be utilized here:

A user might want to get back the initial view of the ListView after playing with the sort buttons, therefore a Clear Sorting button is also added:

To inform the user which direction he or she sorts, ascending and descending images (asc.gif and desc.gif) would be helpful which will be displayed next to the sort buttons:

As a final view of this part, you should see:

Part 5 – With MouseOver/MouseOut Effects
As the ListView control gives more flexibility about displaying the data, MouseOver and MouseOut effects can be easily applied without dealing with some kind of RowCreated (ItemCreated) and RowDataBound (ItemDataBound) events:

Similar approach must also be applied to the AlternatingItemTemplate:

Now, when you mouse over any row in the table, you should have a color of pink:

Update - You can read this post to practice putting dynamic tooltips to the ListViews.
Part 6 – With View All Utility
There may be some cases on the users side that they want to view all rows (results) in one page instead of going through the paging process.

In order to accomplish this, ViewAll and EnablePaging functions will be defined as:

And, View All and Enable Paging buttons will be displayed as:

This post explained some basics of ListView control and its built-in capabilities. The code samples given in here may not be the best approaches but at least can give an idea about how to push the borders of using ListView in the ASP.NET 3.5 applications.
For further hands-on understanding of this post, you can download the source code (by VWD 2008) via here

No comments:

Popular Posts