Asp.Net: DataPager problem with Listview

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

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

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
Asp.Net: DataPager problem with Listview Pieter Brinkman avatarAuthor: Pieter Brinkman ()

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+

Asp.Net: DataPager problem with Listview WebsiteAsp.Net: DataPager problem with Listview TwitterAsp.Net: DataPager problem with Listview Google PlusAsp.Net: DataPager problem with Listview Linkedin

Related posts

Design Patterns(C#): Basic example Strategy pattern
Asp.Net: keyboard sort items
Asp.Net: invoke WCF method with WCF Test Client
Daily Tip: Sitecore caching sliding and absolute

One Response to Asp.Net: DataPager problem with Listview

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

    Click on a tab to select how you'd like to leave your comment

    Leave a Reply

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

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>