How do I use a DataPager with Server Side Paging?

前端 未结 3 1462
灰色年华
灰色年华 2020-12-30 05:46

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



        
3条回答
  •  遥遥无期
    2020-12-30 06:23

    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).

    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 markup:

    
       ......
    
    
    
    
        
    
    
    
    
    

    code-behind:

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

    Note, MSDN states in regards to the QueryStringField attribute:

    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.

    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.

    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.

    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.

提交回复
热议问题