Creating a Sortable Membership

Published 12 July 07 08:54 AM | adrian

I was trying to display a list of ASP.NET members using GridView and ObjectDataSource control. Apparently, the System.Web.Security.Membership class doesn't support paging with GridView natively. It does have GetAllUsers() with paging parameters, but incompatible with GridView control.

For this solution to work, the Membership class has to be changed. The easiest way is to create a subclass. But sadly, Membership is sealed, that means you can't subclass it. So the next available solution is through wrapper class. A wrapper class uses Adapter design pattern. A wrapper class is where you provide new methods, without changing existing code at all.

ObjectDataSource control needs several methods to make paging work. First it requires a select method with two (or more, but not recommended) parameters. Next it requires a count method to calculate total number of pages. All of them are summed up below:

[DataObject()]
public class PageableMembership
{
  [DataObjectMethod(DataObjectMethodType.Select)]
  public MembershipUserCollection GetAllUsers()
  {
    return GetAllUsers(0, GetAllUsersCount());
  }

  [DataObjectMethod(DataObjectMethodType.Select)]
  public MembershipUserCollection GetAllUsers(int pageIndex, int pageSize)
  {
    int totalRecords;
    return Membership.GetAllUsers((int)pageIndex / pageSize, pageSize, out totalRecords);
  }

  public int GetAllUsersCount()
  {
    return Membership.GetAllUsers().Count;
  }
}

I've also added extra attributes to simplify method choice in the Configure Data Source wizard.

Share this post: | | | |
Filed under:

Comments

No Comments