如何以编程方式强制 ListView 显示第一页
我有一个分页的 ASP.NET ListView。显示的数据经过过滤,可以通过表单控制。当过滤器表单更改时,我创建一个新查询,并执行数据绑定。
然而问题是,当我转到下一页并设置过滤器时,ListView 显示“没有返回数据”。这并不奇怪,因为应用过滤器后,只有一页数据。
所以我想做的是重置寻呼机。这是解决问题的正确方法吗?我该怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在我的负载处理程序中使用了这个技巧。如果结果项的数量相同,它不会重置寻呼机,但页面索引仍然有效,所以我现在可以接受。
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.
如果你知道怎么做,那就很简单了。我将以下代码添加到过滤器的
onchange
事件中:If you know how to do it, it is simple. I added the code below to my
onchange
-events of my filter:上述解决方案都是正确的,因为它们都调用相同的方法。我只是认为应该指出,无论您希望更新数据源(即列表或数组等),您都应该调用
yourPagerElement.SetPageProperties(...)
。例如,在应用一些过滤或极大改变列表大小的操作之后。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.如果您的列表视图自动有界(即 ObjectDataSource),您可能会遇到问题
Load 事件处理程序不起作用,因为您还没有新的 DataPager.TotalRowPage 值,但您可以处理 Page_PreRenderComplete,如下所示:
这总是使 ListView 处于包含数据的页面。如果页面大于 TotalRow,则切换到第一页。
注意:我正在使用有效的分页,并且仅返回要显示的数据,因此我需要重新绑定数据源(最后一个参数(true))
缺点:如果 StartRowIndex 大于 TotalRowCount,则双倍数据绑定。
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:
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.