Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force ListView to show first page programmatically

I have a paged ASP.NET ListView. The data shown is filtered, which can be controlled by a form. When the filter form changes, I create a new query, and perform a DataBind.

The problem however, when I go to the next page, and set a filter, the ListView shows "No data was returned". That is not weird, because after the filter is applied, there is only one page of data.

So what I want to do is reset the pager. Is that a correct solution to the problem? And how do I do that?

like image 787
doekman Avatar asked May 17 '10 13:05

doekman


2 Answers

I use this hack in my Load handler. It'll not reset the pager if the number of result items is the same, but the page index will still be valid so I can live with that for now.

if (IsPostBack)
{
    DataPager pgr = MyListView.FindControl("MyPager") as DataPager;
    if (pgr != null && MyListView.Items.Count != pgr.TotalRowCount)
    {
        pgr.SetPageProperties(0, pgr.MaximumRows, false);
    }
}
like image 91
Lloyd Void jr. Avatar answered Sep 20 '22 20:09

Lloyd Void jr.


If you know how to do it, it is simple. I added the code below to my onchange-events of my filter:

DataPager pager = ListViewReference.FindControl("DataPagerId") as DataPager;
if (pager != null)
{
    pager.SetPageProperties(0, pager.PageSize, true);
}
like image 31
doekman Avatar answered Sep 19 '22 20:09

doekman