Asp.Net: DataPager problem with Listview

by Pieter Brinkman 23. December 2009 06:48

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.

[code:c#]

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();
}

[/code]


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

Cheers,

Pieter

Categories: ASP.Net | Controls

Comments disabled

by Pieter Brinkman 23. December 2009 05:06

I disabled the comments because of the huge amounts of spam I'm receiving. So if you have any questions you can contact me trough linked-in.

Sorry for the inconvenience.

Tags:
Categories: