question

ANB avatar image
0 Votes"
ANB asked XuDongPeng-MSFT edited

Datapager and ListView

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

dotnet-aspnet-webforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

XuDongPeng-MSFT avatar image
0 Votes"
XuDongPeng-MSFT answered XuDongPeng-MSFT edited

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.