5,276,156 members and growing! (24,187 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto License: The Code Project Open License (CPOL)

Paging ListView With DataPager

By danfcosta

How to paging ListView with DataPager Programatically
VB (VB 9.0, VB), .NET (.NET, .NET 3.5, .NET 2.0, .NET 3.0), ASP.NET, Dev

Posted: 3 Mar 2008
Updated: 3 Mar 2008
Views: 4,277
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
1 vote for this article.
Popularity: 0.00 Rating: 4.00 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 100.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Recently, I receive some e-mail about ListView from colleagues/friends who want to Paging the ListView, but the DataSource must be set programatically. Then, I do some tests and get the result that follows.

Background

Paging the ListView with DataPager is very easy and you can't write some code. Just add and configure a DataSource, add a ListView, set the DataSource to ListView. Add a DataPager and configure it to ListView. Done!

Run app and enjoy it!

But, when the DataSource is set in code, some cautions must be done. Now, to populate the ListView, you must use the DataPager.

Using the code

Let't try. Add a ListView:

<asp:ListView ID="ListViewProducts" runat="server" ItemPlaceholderID="ProductItem">
    <ItemTemplate>
        <div class="Product">
            <strong>
                <asp:Label runat="server" ID="LabelId" Text='<%# Eval("Id") %>'></asp:Label>
                ::
                <asp:Label runat="server" ID="LabelName" Text='<%# Eval("Name") %>'></asp:Label>
            </strong>
            <br />
            <em>
                <asp:Label runat="server" ID="LabelDescription" Text='<%# Eval("Description") %>'></asp:Label>
            </em>
        </div>
    </ItemTemplate>
    <LayoutTemplate>
        <asp:PlaceHolder runat="server" ID="ProductItem"></asp:PlaceHolder>
    </LayoutTemplate>
    <ItemSeparatorTemplate>
        <hr />
    </ItemSeparatorTemplate>
</asp:ListView> 

Then, add the DataPager:

<asp:DataPager ID="DataPagerProducts" runat="server" PagedControlID="ListViewProducts"
    PageSize="3" OnPreRender="DataPagerProducts_PreRender">
    <Fields>
        <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" />
        <asp:NumericPagerField />
        <asp:NextPreviousPagerField ShowLastPageButton="True" ShowPreviousPageButton="False" />
    </Fields>
</asp:DataPager>

See that the event OnPreRender was implemented. It's used to populate the ListView:

protected void DataPagerProducts_PreRender(object sender, EventArgs e)
{
    ProductList db = new ProductList();
    this.ListViewProducts.DataSource = db.GellAll();
    this.ListViewProducts.DataBind();
}

I create two class to help this example:

1) Product: represent a product item;
2) ProductList: represent a database access layer.

ProductClass

using System;

/// <summary>
/// Class that represent a product item.
/// </summary>
public class Product
{
    private int? _Id;
    private string _Name;
    private string _Descrition;

    public Product(){}

    public Product(int Id, string Name, string Description)
    {
        this._Id = Id;
        this._Name = Name;
        this._Descrition = Description;
    }

    /// <summary>
    /// Product Id
    /// </summary>
    public int? Id
    {
        get { return _Id; }
        set { _Id = value; }
    }

    /// <summary>
    /// Product Name
    /// </summary>
    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

    /// <summary>
    /// Product Complete Description
    /// </summary>
    public string Description
    {
        get { return _Descrition; }
        set { _Descrition = value; }
    }
}

ProductList Class

using System;
using System.Collections.Generic;

/// <summary>
/// Simulation of a Product Database
/// </summary>
public class ProductList
{
    private IList<Product> _ProductDB = new List<Product>();

    public ProductList()
    {
        this._ProductDB.Add(new Product(1, "Computer", "Complete hardware with software included."));
        this._ProductDB.Add(new Product(2, "Kitchen Calendar", "Beautiful caledar for your kitchen."));
        this._ProductDB.Add(new Product(3, "Shoes", "Most advanced anti-impact system in a small shoe."));
        this._ProductDB.Add(new Product(4, "Pen", "What you think, must be written. This pen is your helper."));
        this._ProductDB.Add(new Product(5, "Cell Phone", "Powerfull comunication thing. Today is part of your body. Get one more."));
    }

    public IList<Product> GellAll()
    {
        return this._ProductDB;
    }
} 

Points of Interest

Interesting Articles:

  • http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datapager.aspx
  • http://aspnet.4guysfromrolla.com/articles/021308-1.aspx
  • http://www.west-wind.com/WebLog/posts/127340.aspx

History

2008-03-03 - First insert.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

danfcosta



Occupation: Architect
Location: Brazil Brazil

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
Subject  Author Date 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Mar 2008
Editor:
Copyright 2008 by danfcosta
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project