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" OnSelecting="LinqDataSource1_Selecting"> </asp:LinqDataSource>
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e) { // Assuming that GetDataSource method returns the datasource. e.Result = GetDataSource(); }
Now you can save your hours.
Thanks for the info, that was useful!
thx, it was very very useful.
Thanks a lot! You just found a place inside my rss reader ;-)
Thank you sooooooooooooooooooooooo much. I save 1000 hours.
Thanks, this worked great!
Thank you very much. Take a look at this too.
http://nayyeri.net/blog/using-datapager-and-listview-controls-with-a-custom-data-source-in-the-asp.net/
Good Job! I really need this trick!
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.
You, my friend. Rock!
Thanks..
Good tip! Now I have a user control that contains a DataPager, a ListView, and a DropDownList for filtering…and it all works. Cheers!
it would be gr8 if u paste the Getsource method code,which would give more clear idea
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.