M5A B4N

Personal Blog of Mustafa Basgun

ListView Control in ASP.NET 3.5 – 1

with 11 comments

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:

Conclusion

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.

Download

For further hands-on understanding of this post, you can download the source code (by VWD 2008) via here.

Update

This post is continued by:

ListView Control in ASP.NET 3.5 – 2
ListView Control in ASP.NET 3.5 – 3
ListView Control in ASP.NET 3.5 – 4

About these ads

Written by Mustafa Basgun

December 27, 2007 at 5:00 PM

11 Responses

Subscribe to comments with RSS.

  1. [...] Using the ListView Control in ASP.NET 3.5: Mustafa Basgun has also written some great articles that drill into the new ListView control further.  Read Part 1, Part 2, Part 3, Part 4, and his Locking ListView’s Header and Footer Columns with CSS to learn more about it. [...]

  2. [...] Using the ListView Control in ASP.NET 3.5: Mustafa Basgun has also written some great articles that drill into the new ListView control further.  Read Part 1, Part 2, Part 3, Part 4, and his Locking ListView’s Header and Footer Columns with CSS to learn more about it. [...]

  3. [...] Uso del control ListView en ASP.NET 3.5: Mustafa Basgun también ha escrito algunos artículos sobre el nuevo control ListView. Leedlos 1, 2, 3, 4. [...]

  4. [...] Using the ListView Control in ASP.NET 3.5: Mustafa Basgun escreveu alguns excelentes artigos que mergulham no novo controle ListView. Leia a Parte 1, Parte 2, Parte 3, Parte 4 e seu Locking ListView's Header and Footer Columns with CSS para aprender mais sobre ele. [...]

  5. Hi Mustafa,

    I gain a lot from this article within a short time. Thanks for uploading such a valuable article.

    Regards,

    Tejesh

    Tejesh

    April 1, 2008 at 2:08 AM

  6. [...] ListView Control in ASP.NET 3.5 – 1 « Yet Another Blog on ASP.NET and Some Other Stuff Decent series of posts on using the ListView control…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 th (tags: dev dotnet aspnet) [...]

  7. Hi Mustafa,

    Many thanks for your nice didactic examples on ASP.NET ListView in this article. They helped me a lot.

    Best Regards,

    - Calin

    Calin Stanescu

    October 22, 2009 at 5:47 AM

  8. [...] Using the ListView Control in ASP.NET 3.5: Mustafa Basgun has also written some great articles that drill into the new ListView control further.  Read Part 1, Part 2, Part 3, Part 4, and his Locking ListView’s Header and Footer Columns with CSS to learn more about it. [...]

  9. [...] ListView Control in ASP.NET 3.5 [...]

    ASP.NET ListView

    September 30, 2010 at 3:41 AM

  10. Hi,

    This is the exact article that I’m looking for. In my project, I need a similar one where I need to use a ListView control in C#. Very nice article. Thanks for sharing with us. Following link also helped me to complete my task:

    http://www.mindstick.com/Articles/39d30fac-2fe1-4306-9e23-3f9be34cac83/?ListView%20Control%20in%20C#.Net

    Pravesh Singh

    February 25, 2012 at 5:12 AM


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: