Extend repeater to support Datapager

Repeater 2.0

Repeater is a databound control. In the terms of layout and desing, repeater is the most flexible control found on asp.net 2.0. You can completely customize the layout and desing with the tatal controls on html tags genereated on the browser. This type of flexiblity is not found on the other data boudn controls like gridview, datalist, etc.

Instead Repeater 2.0 doesnot supports built-in paging and sorting functionality like gridview. In this article you will know how can we extend Repeater 2.0 to support paging with the DataPager 3.5.

Datapager 3.5

Datapager 3.5 comes with .net framework 3.5 which is a seperate control to integrate with databound controls to support paging funtionalities. The requirement of DataPager can be associated with any databound controls that is implemented from System.Web.UI.WebControls.IPageableItemContainer Interface.  .net framework provides only one control ListView (which is implemented from System.Web.UI.WebControls.IPageableItemContainer) to support with DataPager.

Extend Repeater 2.0 to associate with DataPager

Here are some steps you can follow to extend Repeater 2.0 or any DataBound controls to support paging with DataPager.

1. Create a custom web control.

2. Inherit it from the Repeater and implement System.Web.UI.WebControls.IPageableItemContainer.

3. Implement System.Web.UI.WebControls.IPageableItemContainer methods

4. Drag and drop the Custom Repeater and DataPager control and associate Custom Repeater  to the DataPager.

5. Create Object Datasource and use for repeater as datasource. Need to enable Paging on Object Data source.

The Following Code snnipet shows you how you can extend repeater and implement methods of System.Web.UI.WebControls.IPageableItemContainer to Support paging with DataPager.

using System;

using System.Web.UI;

using System.Web.UI.WebControls;
namespace RepeaterX

{

public class RepeaterExt : Repeater,  IPageableItemContainer

{

public int MaximumRows

{

get

{

return ViewState["_maximumRows"] != null ?

(int)ViewState["_maximumRows"] : -1;

}

}

public int StartRowIndex

{

get

{

return ViewState["_startRowIndex"] != null ?

(int)ViewState["_startRowIndex"] : -1;

}

}

public void SetPageProperties(int startRowIndex, int maximumRows, bool databind)

{

ViewState["_startRowIndex"] = startRowIndex;

ViewState["_maximumRows"] = maximumRows;

if (TotalRowCountAvailable != null)

{

TotalRowCountAvailable(this,

new PageEventArgs((int)ViewState["_startRowIndex"],

(int)ViewState["_maximumRows"], 100));

}

}

protected override DataSourceSelectArguments CreateDataSourceSelectArguments()

{

var e = base.CreateDataSourceSelectArguments();

e.StartRowIndex = StartRowIndex;

e.MaximumRows = MaximumRows;

return e;

}

public event EventHandler<PageEventArgs> TotalRowCountAvailable;

}

}

Usage on WebPage:

 <%@ Register assembly="RepeaterX" namespace="RepeaterX" tagprefix="cc1" %>
<cc1:RepeaterExt ID="RepeaterExt1" runat="server"

DataSourceID="ObjectDataSource1">

<ItemTemplate>

<%# Eval("UserName") %>

<br />

<hr />

</ItemTemplate>

</cc1:RepeaterExt>

<asp:ObjectDataSource ID="ObjectDataSource1" EnablePaging="true" runat="server"

SelectMethod="GetUsers" SelectCountMethod="CountUsers" TypeName="DataProvider">

</asp:ObjectDataSource>

<asp:DataPager ID="DataPager1" PageSize="10" QueryStringField="userpage" runat="server" PagedControlID="RepeaterExt1">

<Fields>

<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"

ShowLastPageButton="True" />

</Fields>

</asp:DataPager>

DataProvider Class (Put this on app_code)

using System.Collections.Generic;

public class DataProvider

{

public static IList<User> GetUsers(int startRowIndex, int maximumRows)

{

IList<User> users = new List<User>();

for (int i = startRowIndex + 1; i < startRowIndex + maximumRows + 1; i++)

{

users.Add(new User() { UserId = i, UserName = "user " + i });

}

return users;

}

public static int CountUsers()

{

return 100;

}

}

public class User

{

public int UserId

{

get;

set;

}

public string UserName

{

get;

set;

}

}

Components used:

1. Repeater (.net 2.0)

2. Object Data source (.net 2.0)

3. DataPager(.net 3.5)

You can find a way to dynamically get TotalRowCount instead of constant 100 value.

, , , , ,

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Share to Facebook Share to Twitter Stumble It Share on Tumblr Digg More...