Datapager and ListView

ANB 181 Reputation points
2022-09-07T23:55:57.273+00:00

I have a page with a ListView, a Datapager and a Dropdown.
This ListView can be populated with 3 different similar datasources ("Source1", "Source2", "Source3").
By default ListView uses Source1.

If I keep using the datapager, everything works perfectly.
Now if I use the Dropdown to change the datasource, the ListView has the new data succesfully and I push the datapager to the first page. Everything works 100%.

The problem now is, for source2 and source3, the datapager doesnt move further than page2.
Why this happen ?

protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{

        myDataPager.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);  
        this.BindSource(ViewState["source"].ToString());  

    }  

    protected void MyDropdownSource_Changed(object sender, EventArgs e)  
    {  
        var ddlSource = MyDropdownSource.SelectedItem.Value;  

        switch (ddlSource)  
        {  
            case "1":  
                myDataPager.SetPageProperties(0, 15, true); //send to the first page datapager  
                ViewState["source"] = "source1";                       
                this.BindSource("source1");  
                break;  

            case "2":  
                myDataPager2.SetPageProperties(0, 15, false); //send to the first page datapager  
                ViewState["source"] = "source2";                       
                this.BindSource("source2");  
                break;  

...

Thx

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. XuDong Peng-MSFT 10,101 Reputation points Microsoft Vendor
    2022-09-08T08:04:07.383+00:00

    Hi @ANB ,

    The problem now is, for source2 and source3, the datapager doesnt move further than page2. Why this happen ?

    According to your description, what you mean is that when you switch the datasource of the ListView control, DataPaper cannot implement the paging function, all data are displayed on one page, right? If this is the case, I have reproduced the issue with the partial code you provided. It is caused by the postback of the page, you can fix this by resetting the pagination parameters after binding the datasource.

    Something like this:

       protected void MyDropdownSource_Changed(object sender, EventArgs e)  
               {  
                   var ddlSource = MyDropdownSource.SelectedItem.Value;  
                   switch (ddlSource)  
                   {  
                       case "1":  
                           DataPager1.SetPageProperties(0, 15, true); //send to the first page datapager  
                           ViewState["Listview1Source"] = "source1";  
                           this.BindListView("source1");  
                           DataPager1.PageSize = 4;  
                           break;  
                       case "2":  
                           DataPager1.SetPageProperties(0, 15, false); //send to the first page datapager  
                           ViewState["Listview1Source"] = "source2";  
                           this.BindListView("source2");  
                           DataPager1.PageSize = 4;  
                           break;  
                       case "3":  
                           DataPager1.SetPageProperties(0, 15, false); //send to the first page datapager  
                           ViewState["Listview1Source"] = "source3";  
                           this.BindListView("source3");  
                           DataPager1.PageSize = 4;  
                           break;  
                   }  
               }  
    

    Result:

    ZcapO.gif

    As you can see, I set PageSize to 4, you can modify accordingly according to your own situation. But if I misunderstood anything, just let me know.

    Best regards,
    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments