我有一个分页的 ASP.NET ListView。显示的数据经过过滤,可以通过表单进行控制。当过滤器表单发生变化时,我会创建一个新查询并执行 DataBind。
然而问题是,当我转到下一页并设置过滤器时,ListView 显示“没有返回数据”。这并不奇怪,因为应用过滤器后,只有一页数据。
所以我想做的是重置寻呼机。这是解决问题的正确方法吗?我该怎么做?
我在我的负载处理程序中使用了这个 hack。如果结果项的数量相同,它不会重置寻呼机,但页面索引仍然有效,所以我现在可以忍受。
if (IsPostBack)
{
DataPager pgr = MyListView.FindControl("MyPager") as DataPager;
if (pgr != null && MyListView.Items.Count != pgr.TotalRowCount)
{
pgr.SetPageProperties(0, pgr.MaximumRows, false);
}
}
如果你知道怎么做,那很简单。我将以下代码添加到onchange
过滤器的 -events 中:
DataPager pager = ListViewReference.FindControl("DataPagerId") as DataPager;
if (pager != null)
{
pager.SetPageProperties(0, pager.PageSize, true);
}
上述任何一种解决方案都是正确的,因为它们都调用了相同的方法。我只是认为应该指出你应该调用yourPagerElement.SetPageProperties(...)
你想要更新数据源(即列表或数组等)的任何位置。例如,在应用了一些过滤或极大地改变列表大小的东西之后。
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.