all things Sitecore related

Asp.Net: DataPager problem with Listview

By on December 23, 2009 in ASP.Net, Controls with 2 Comments

When using the Datapager with a ListView I had the following problem. When clicking a paging button for the first time nothing happens.But when I click a button the second time, then the page from the first click loads.

I search the internet for a solution and found that you need to add some code to the OnPagePropertiesChanging event of the list view to reload the DataPager.

The following code is the solution to my problem. Including a fix that the data doesn’t get loaded two times.


private List<Product> productList;

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        fillGrid();
}

private void fillGrid()
{
    if(productList == null)
        productList = getproducts();
    ListView1.DataSource = productList;
    ListView1.DataBind();
    DataPager1.DataBind();
}

public List<Product> getproducts()
{
    using (AdventureWrksDataContext db = new AdventureWrksDataContext())
    {
        return db.Products.ToList();
    }
}

protected void lvproducts_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    fillGrid();
}


You can download the solution (PagingExample.zip (83.05 kb))

Cheers,

Pieter

Tags: , , ,

About the Author

About the Author: Pieter is Technical Marketing Manager for Sitecore Netherlands and owner of Gaatverweg.nl. He has more than ten years experience with software developing in multiple programming languages and with different Content Management Systems. Before joining Sitecore Pieter was a lead developer for multiple Sitecore and .Net projects, he joined Sitecore in 2011 as an Solution Architect in The Netherlands, after two years as an Solution Architect he joined the Technical Marketing department. In the role as Techinical Marketing Manager he is responsible for the Global MVP program and the Sitecore technical branding strategy. You can follow Pieter on twitter: @pieterbrink123 or Google+ .

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

There Are 2 Brilliant Comments

Trackback URL | Comments RSS Feed

  1. Faizan Atiq says:

    Hi,
    Thanks for the Code, i was searching for this problem for a long long time. Again I appreciate you work Thank you Again!!

  2. Julian Karapetkov says:

    Ohhh :) God Bless you, man. I was trying to fix that bug for a couple of hours. Finally I made it – thanks to your post :)

    Thanks a lot!

Post a Comment

Your email address will not be published. Required fields are marked *

Top