如何以编程方式强制 ListView 显示第一页

发布于 2024-09-02 00:46:45 字数 191 浏览 5 评论 0原文

我有一个分页的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

地狱即天堂 2024-09-09 00:46:45

我在我的负载处理程序中使用了这个技巧。如果结果项的数量相同,它不会重置寻呼机,但页面索引仍然有效,所以我现在可以接受。

if (IsPostBack)
{
    DataPager pgr = MyListView.FindControl("MyPager") as DataPager;
    if (pgr != null && MyListView.Items.Count != pgr.TotalRowCount)
    {
        pgr.SetPageProperties(0, pgr.MaximumRows, false);
    }
}

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);
    }
}
恋竹姑娘 2024-09-09 00:46:45

如果你知道怎么做,那就很简单了。我将以下代码添加到过滤器的 onchange 事件中:

DataPager pager = ListViewReference.FindControl("DataPagerId") as DataPager;
if (pager != null)
{
    pager.SetPageProperties(0, pager.PageSize, true);
}

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);
}
爱已欠费 2024-09-09 00:46:45

上述解决方案都是正确的,因为它们都调用相同的方法。我只是认为应该指出,无论您希望更新数据源(即列表或数组等),您都应该调用 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.

蔚蓝源自深海 2024-09-09 00:46:45

如果您的列表视图自动有界(即 ObjectDataSource),您可能会遇到问题

Load 事件处理程序不起作用,因为您还没有新的 DataPager.TotalRowPage 值,但您可以处理 Page_PreRenderComplete,如下所示:

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

这总是使 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:

    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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文