Creating a Repeater that Supports Pagination and Sorting, Part 2By Rachael Schoenbaum
In Part 1 we started our overview of the SortablePaginationRepeater control, examining the added properties and how to add paging support. In this final part of the article we'll see how to add sorting support and look at an example of the pageable, sortable Repeater in an ASP.NET Web page.
Setting Up the Object for Sorting
To provide sorting the page developer will need to specify what columns are sortable and how these sortable columns should be displayed in the Repeater. They'll do this through markup in the SortablePaginationRepeater's declarative syntax. For example, when creating a SortablePaginationRepeater that has three sortable columns the declarative syntax might look like:
In order to achieve this desired declarative syntax, the first thing we need is a separate class used to define the tag
that contains the sorting information (i.e.,
<SortColumn>). The class is
comprised of two string properties,
ColumnName property provides the name (and direction, if necessary) that the results should
be sorted by. The
DisplayName is the text that should appear to the end user.
There are two fundamental ways to sort data in a Web page:
- Require a postback to the server, which will requery the data in resorted order, resending the sorted content back to the client, or
- Perform the sorting on the client using client-side script. This entails having the complete contents being paged through available on the client through hidden form fields or hidden HTML sections, and having the ability to show/hide/reorder the data on the fly with client-side techniques. (For example, the 4Guys article Paging Records on the Client shows how to accomplish complete, client-side paging in classic ASP.)
Displaying the Pagination and Sorting Controls
Once the SortablePaginationRepeater has a paginated data source and sorting is in place, we must display the various pagination and sorting controls. Displaying the controls is comprised of two parts:
- Create the Paging/Sorting Controls – the user interface used to change the page the user is on must be created and
added to the SortablePaginationRepeater's control hierarchy. This is done in the
OnPreRendermethod fires during a control's
OnPreRender()method. Calling the base class's
OnPreRender()method ensures that the base class (the Repeater class, for our control) gets an opportunity to run the code associated with its
Rendering the Control
Each time an ASP.NET page is requested, be it on the first visit or on subsequent postbacks, the controls that makeup the page are rendered. Rendering is the process of generating the appropriate HTML markup for each control. For the SortablePaginationRepeater control the markup that needs to be generated includes not only the base Repeater, but also the paging and sorting UI elements. Generating markup in a server control is not as easy as just squirting out one big HTML string. Rather, we create instances of the classes that correspond to the HTML elements we want displayed and add these to the server control's
Controlscollection. The actual generation of HTML markup is performed for us, behind the scenes. Here's a snippet of code for rendering an HTML table by creating a
Note the last 15 or so lines of code in the above subroutine. Everything prior to that sets up the HTML that will be
displayed. After the controls are put into HTML objects, the
Render() method determines if that HTML should
be displayed above the repeater or below the Repeater.
Perhaps the most interesting thing of all is that the while the controls require a special rendering, the Repeater
itself is rendered as-is, using the native implementation (i.e., via the call to
DataSource provided to a
PagedDataSource, .NET inherently provides the logic
necessary to display only the page in question.
Using the SortablePaginationRepeater Control in an ASP.NET Web Page
To use the control in an ASP.NET page, you must first register it. This is a similar concept to registering a user control. After the
@Pagedeclaration, add the following line:
To call the control in your ASP.NET page, make sure you use the tag prefix you defined when you registered the control and you should use the name of class after the colon. See below for an example.
Note that by supplying the sort column tags, you are telling the control to include a sort control. If you don't provide this tag, there will be no sort control. Also note that it is possible to provide values for all the other public properties, even though they may not be shown in this example. By not providing the value, you accept the default value defined in the control's constructor.
About the Author
Rachael Schoenbaum is a developer specializing in ASP, ASP.NET, Visual Basic, SQL Server, XML, and related technologies. She consults for Lucidea and has been programming since 1999.