我如何使用与服务器端分页一个DataPager的? [英] How do I use a DataPager with Server Side Paging?

查看:195
本文介绍了我如何使用与服务器端分页一个DataPager的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个DataPager的做服务器端分页。这里是我的code

I'm trying to use a DataPager to do Server Side paging. Here is my code

<asp:DataPager ID="pgrFooBars" PagedControlID="lvFooBars" 
    QueryStringField="page" runat="server" >
<Fields>
    <asp:NumericPagerField />
</Fields>
</asp:DataPager>

code背后

protected void Page_Load(object sender, EventArgs e)
{
    ConfigureBlogPostListView();
}

private void ConfigureBlogPostListView()
{
    int pageNum;
    int.TryParse(Request.Params["page"], out pageNum);
    int pageSize = 20;

    PagedList<IFooBar> FooBars = FooService.GetPagedFooBars(
        new PagingSettings(pageNum, pageSize));

    ListViewPagedDataSource ds = new ListViewPagedDataSource();
    ds.AllowServerPaging = true;
    ds.DataSource = FooBars;
    ds.MaximumRows = pageSize;
    ds.StartRowIndex = pageNum;
    //TotalCount is the total number of records in the entire set, not just those loaded.
    ds.TotalRowCount = FooBars.TotalCount;

    lvFooBars.DataSource = ds;
    lvFooBars.DataBind();

    pgrFooBars.PageSize = pageSize;
    pgrFooBars.SetPageProperties(pageNum, FooBars.TotalCount, true);
}

PagedList来自RobConery的有用的帖子<一个href=\"http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/\">http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/.

问题是,DataPager的似乎使用ListView控件的Count属性来确定的记录总数,在这种情况下为20。不知何故,它需要知道,有1500,不是共20个记录。 DataPager的有一个属​​性TotalRowCount,但这是只读的。

The problem is that the DataPager appears to be using the Count property of the ListView to determine the total number of records, which in this case is 20. Somehow, it needs to know that there are 1,500, not 20 total records. The DataPager has a property TotalRowCount, but this is read-only.

我从来没有看到与服务器端分页一个DataPager的例子​​,但认为它可以做服务器端分页,否则什么好是QueryStringField属性?

I have never seen a DataPager example with Server Side paging, but assumed that it could do Server Side Paging, otherwise what good is the QueryStringField attribute?

据我所知,您可以使用的方法,如4GuysFromRolla在这里做<一个做一个自定义分页解决方案href=\"http://www.4guysfromrolla.com/articles/031506-1.aspx\">http://www.4guysfromrolla.com/articles/031506-1.aspx,但我首先想知道如果与DataPager的解决方案创建一个自定义解决方案之前,是可能的。

I am aware that you can do a custom paging solution using methodology like the 4GuysFromRolla did here http://www.4guysfromrolla.com/articles/031506-1.aspx, but I'd first like to know if a solution with the DataPager is possible before creating a custom solution.

更新
更我看看这个,越觉得我来的结论是,这是不可能的,而且,不幸的是,DataPager的是控制意味着只有很小的网站。我想要做的确实应该很简单,如果控制被正确建立。我想可以说

UPDATE The more I look at this, the more that I'm coming to the conclusion that this is not possible and that, unfortunately, the datapager is a control meant for small web sites only. What I want to do should really be quite simple if the control were built correctly. I want to be able to say

dpFooBars.TotalRowCountComputed = false;
dpFooBars.TotalRowCount = AnyNumberThatISoChoose;

我一直在寻找一些黑客来完成同样的事情,但现在看来,DataPager的的TotalRowCount是从它绑定到数据源项目的实际数量计算的。这似乎很奇怪,我认为微软将在同一时间创建一个ListViewPagedDataSource()类和DataPager的,而不是让他们正确地一起工作,但是这似乎已经发生了什么。

I've been looking for some hack to accomplish the same thing, but it appears that the datapager's TotalRowCount is computed from the actual number of items in the datasource that it's bound to. It seems very odd to me that Microsoft would create a ListViewPagedDataSource() class and a DataPager at the same time and not have them work correctly together, but this appears to have been what has happened.

更新2(AHA时刻吗?)
看来,它已通过使用一个ObjectDataSource和定制SelectCountMethod是不可能做服务器端分页,因为NET 2.0()。我认为它应该是可以自定义的ObjectDataSource,以满足我的需求。嗯。我要离开这个周末,所以这将是一两天给我看看,如果这个工程。敬请关注,真正的信徒。

UPDATE 2 (AHA MOMENT?) It seems that it has been possible to do server side paging since .Net 2.0 by using an ObjectDataSource and customizing the SelectCountMethod(). I believe it should be possible to customize ObjectDataSource to suit my needs. Hmmm. I'm going away for the weekend, so it'll be a couple of days for me to see if this works. Stay tuned, true believers.

推荐答案

这似乎获得服务器端分页的DataPager的工作的唯一方法是使用的LinqDataSource 在您的标记(更新:这不是真的,见下文的),并设置你的ListView的的DataSourceID(如<一个href=\"http://weblogs.asp.net/scottgu/archive/2007/08/10/the-asp-listview-control-part-1-building-a-product-listing-page-with-clean-css-ui.aspx\"相对=nofollow> ScottGu的样本 - 第6步),而不是通过在ListView DataSource属性。不过我也发现,有一个的使这个更可行,所以你可以通过code-后面,而不是在标记(注意,文章表示,这将与任何数据源控件工作,但我不认为它定义你的LinqDataSource查询是的情况下)。

It seems the only way to get server-side paging to work with the DataPager is to use a LinqDataSource in your markup (update: this is not true, see below), and set the DataSourceID of your ListView (as in ScottGu's sample - step 6), not via the ListView DataSource property. However I did discover that there is a trick to make this more workable so you can define your LinqDataSource query via code-behind rather than in the markup (note, the article says this will work with any DataSource control but I don't think it is the case).

这可能不会对你的情况有任何帮助,因为我注意到,您的呼叫某种服务,可能不会(也不应该)将返回一个IQueryable的结果这是必要的这个工作的。这无论如何是在如果你有兴趣...

This will probably not be any help for your situation as I noticed that your calling some sort of service that probably won't (and shouldn't) be returning an IQueryable result which is necessary for this to work. Here it is anyway in-case you're interested ...

ASPX标记:

<asp:ListView ID="lvFooBars" DataSourceID="dsLinq" ....>
   ......
</asp:ListView>

<asp:DataPager ID="pgrFooBars" PagedControlID="lvFooBars" 
    QueryStringField="page" runat="server" >
<Fields>
    <asp:NumericPagerField />
</Fields>
</asp:DataPager>

<asp:LinqDataSource id="dsLinq" runat="server" OnSelecting="dsLinq_Selecting" />

code-背后:

protected void dsLinq_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    //notice this method on FooService requires no paging variables
    e.Result = FooService.GetIQueryableFooBars(); 
}

请注意,<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datapager.querystringfield.aspx\"相对=nofollow> MSDN 在问候了 QueryStringField 属性状态:

Note, MSDN states in regards to the QueryStringField attribute:

设置该属性是,如果你有用
  希望有数据的所有页面
  由搜索引擎索引。这个
  是因为控制产生
  不同的URL数据的每一页。

Setting this property is useful if you want to have all the pages of data indexed by a search engine. This occurs because the control produces a different URL for each page of data.

更新:逸岸你能得到这个使用ObjectDataSource控件,而不是LinqDataSource控件的工作 - 看<一个href=\"http://mosesofegypt.net/post/Building-Custom-Paging-with-LINQ2c-ListView2c-DataPager-and-ObjectDataSource.aspx\"相对=nofollow>本文以及下载示例。在使用一个ObjectDataSource是几乎没有使用LinqDataSource控件那样简单,它使用较少的魔法LINQ的东西(而不是它使用魔法字符串映射到业务/数据层方法堆),并允许IEnumerable的曝光为您的数据访问方法而不是IQueryable的。

Update: infact you can get this to working using an ObjectDataSource control instead of the LinqDataSource control - see this article and download the sample. While using an ObjectDataSource isn't nearly as simple as using a LinqDataSource control, it's uses less magic linq stuff (instead it uses heaps of magic strings that map to business/data layer methods) and allows the exposure of IEnumerable for your data access method instead of IQueryable.

不过,我从来没有真正嵌入在我的UI标记任何形式的DataSource控件的(我相信这是必要的)的粉丝,所以我可能会避开,除了小型应用DataPager控件,因为你在建议您第一次更新。

Still, I have never really a fan of embedding any sort of DataSource control in my UI markup (I believe this is necessary), so I would probably steer clear of the DataPager control except for small applications as you have suggested in your first update.

约翰,我注意到了另一件事情是,你想嫁给标准Asp.Net服务器控件(依靠的ViewState)与被开发为Asp.Net MVC应用一个辅助类的PagedList类。虽然这可以潜在地工作,有可能是更简单的途径服用。

John, one other thing I noticed was that you are trying to marry standard Asp.Net server controls (that rely on ViewState) with the PagedList class which was developed as a helper class for Asp.Net Mvc applications. While this can potentially work, there may be simpler routes to take.

这篇关于我如何使用与服务器端分页一个DataPager的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆