Using DataPager with Code-Behind Data Source

Some of you might have already realized that DataPager does its job only if you use a DataSource control. For instance, if you want to set a ListView’s DataSource property in page’s Load event and expect DataPager control to successfully page the ListView control, it means that you will need to spend your hours to find why it does not work.

Even though I still can’t figure out why it does not work, here is the workaround. Just create a DataSource control (e.g.: LinqDataSource) control just with “ID” and “runat” properties and then hook the “Selecting” event. Also set the ListView’s “DataSourceID” property to the ID of the DataSource control. What you will do there is to set the “Result” property of the SelectEventArgs argument.

<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    // Assuming that GetDataSource method returns the datasource.
    e.Result = GetDataSource();

Now you can save your hours.

You may also like...

12 Responses

  1. Arnaud says:

    Thanks for the info, that was useful!

  2. says:

    thx, it was very very useful.

  3. Thanks a lot! You just found a place inside my rss reader ;-)

  4. Ahmad says:

    Thank you sooooooooooooooooooooooo much. I save 1000 hours.

  5. Josh says:

    Thanks, this worked great!

  6. Rambod says:

    Good Job! I really need this trick!

  7. Amílcar Rodrigues says:

    Thanks for this unbelievable, but so simple solution. I think it is important to say that GetDataSource() function must return as IEnumerable. Finaly, I agree with you this approach save me handreds of hours.

  8. Raul says:

    You, my friend. Rock!


  9. wloescher says:

    Good tip! Now I have a user control that contains a DataPager, a ListView, and a DropDownList for filtering…and it all works. Cheers!

  10. Swapnil Shintre says:

    it would be gr8 if u paste the Getsource method code,which would give more clear idea

  11. Coskun Sunali says:

    Hi Swapnil,

    As Amílcar Rodrigues mentioned above, the GetDataSource must return an IEnumerable.

    How you retrieve that IEnumerable collection is pretty much up to you and how you store your data.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.