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.

 

Components used:

1. Repeater (.net 2.0)

2. Object Data source (.net 2.0)

3. DataPager(.net 3.5)

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

		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 TotalRowCountAvailable;

}

}
 

Usage on WebPage:

		
 <%@ Register assembly="RepeaterX" namespace="RepeaterX" tagprefix="cc1" %>




<%# Eval("UserName") %>



DataProvider Class (Put this on app_code)

		

using System.Collections.Generic;

public class DataProvider

{

public static IList GetUsers(int startRowIndex, int maximumRows)

{

IList users = new List();

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;

}

} 
blog comments powered by Disqus