How to force ListView to show first page programmatically

拥有回忆 提交于 2020-01-01 05:31:19

问题


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?


回答1:


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



回答2:


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



回答3:


Either of the above solutions are correct as they both call the same method. I just think it should be pointed out that you should call yourPagerElement.SetPageProperties(...) wherever you want your datasource (ie a list or an array, etc.) to be updated. For example, after applying some filtration or something that greatly changes the list size.




回答4:


You may have problems if your listview is automatically bounded (i.e. ObjectDataSource)

Load event handler doesn't work, because you haven't yet the new DataPager.TotalRowPage value, but you can handle Page_PreRenderComplete, like this:

    protected void Page_PreRenderComplete(object sender, EventArgs e)
    {
        // PreRenderComplete occurs just after databindings page events
        // And saves to viewstate

        // Trick on search to avoid "No data" on results when old page is greater than actual row count                
        if (DataPager1.StartRowIndex  > DataPager1.TotalRowCount )
            DataPager1.SetPageProperties(0, DataPager1.MaximumRows, true);
    }

This always leaves ListView in a page with data. If page is greater than TotalRow, switch to first page.

Notes: I'm using efective paging, and returning only the data to be displayed, so I need to rebind de datasource (last parameter (true))

Cons: Double databind if StartRowIndex is greater than TotalRowCount.



来源:https://stackoverflow.com/questions/2849449/how-to-force-listview-to-show-first-page-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!