This project is read-only.

Paging in N2

Mar 13, 2009 at 10:31 AM
Edited Mar 13, 2009 at 10:41 AM
I know their is an Advanced Pager add-on available in the download section, but is their any kind of paging by default in N2 available? If so, how does it work? I can't find anything about that.
I'm experimenting with the templates project. What will happen if I post 100 new news items on the news page? I've posted 15 news items and they appear all on the same page. Their is no paging. :-(

Thanks!
Mar 13, 2009 at 11:42 AM
Edited Mar 13, 2009 at 11:46 AM
N2.Templates is more of a sample than a world-ready product. I think it makes sense to design your own templates (that is: aspx/ascx files) for existing N2.Templates.Items you want to utilise. This way you're almost free to implement paging in whatever way you like. Say, you might want to go with a standard <asp:ListView/> and <asp:ObjectDataSource/> :

Your template:

<script runat="server">
protected void ds_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = this.CurrentItem;
}
</script>

<asp:ObjectDataSource
runat = "server"
ID = "ds"
SelectMethod = "GetItems"
SelectCountMethod = "GetItemCount"
TypeName = "Items.MyItem"
DataObjectTypeName = "Items.MyChild"
OnObjectCreating = "ds_ObjectCreating"
EnablePaging = "True">

<asp:ListView
ID="lv"
DataKeyNames="ID"
runat="server"
DataSourceID="ds">
<%-- templates --%>
</asp:ListView>

<asp:DataPager
ID="dp"
PagedControlID="lv"
PageSize="15"
runat="server">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>

Now your item definition:

using System.ComponentModel;
using System.Linq;
namespace Items {
[Definition][DataObject]
public class MyItem: ContentItem
{
[DataObjectMethod(DataObjectMethodType.Select, true)]
public IEnumerable<MyChild> GetItems()
{
return this.GetChildren().OfType<MyChild>();//filter as needed
}
[DataObjectMethod(DataObjectMethodType.Select, true)]
public IEnumerable<MyChild> GetItems(int maximumRows, int startRowIndex)
{
return this.GetItems()
.Skip(startRowIndex)
.Take(maximumRows);
}

public int GetItemCount(){return this.GetItems().Count();}
}
}

That's not a perfect example from the performance perspective, but hopefully you've got the idea ;-)