How the DataGridPager Control Works
Starting at the top of the DataGridPager.vb
class file in Listing 1
, notice that the DataGridPager control is derived from the System.Web.UI.Control object. This object contains all the functionality the DataGridPager control needs.
The DataGridPager works by dynamically creating LinkButton controls and adding them to its child Controls
collection. This means the DataGridPager is a composite control because it contains child controls. Microsoft specifies
that composite controls must implement the INamingContainer interface and override the CreateChildControls
() method. This is necessary so that when the control's containing page runs, it will call the control's CreateChildControls()
method to allow the control to recreate its child controls. It's vital because the page must recreate the child controls during postbacks so their Click()
events can be handled properly. Look at the following code from the "Overrides" region:
Protected Overrides Sub CreateChildControls()
' Builds child pager buttons so their click events can
' be handled
If TypeOf _dg Is DataGrid Then BuildPagerButtons()
The preceding code simply recreates the paging buttons exactly as they were when last sent to the browser whenever the DataGridPager control is attached to a DataGrid.
Continuing through the code for Listing 1
, you'll notice that this control also overrides the OnInit
() and Render
() methods. OnInit()
initializes the _dg
member variable to point to the DataGrid object. The overridden Render()
method allows the control to write out an error message to the page if the control is unable to find the DataGrid specified in its DataGridToAttachTo
The next two regions, "Member Variables" and "Properties" implement the control's properties. These properties set or return the values from private variables within the class.
The final region, "Private Routines", contains the BuildPagerButtons()
() method is the backbone of this control. This method creates and configures the paging link buttons and adds them to the control. Note that the control exposes properties (NextPageLinkText
) that you can use to specify the text for these controls. Also note that the code sets each LinkButton's CommandName
properties, which the application needs when a user clicks one of the links.
Clicking one of the pager LinkButtons calls the PagerLinkButtonClick
() method, which uses the clicked LinkButton's CommandName
to determine which paging action to take. After the DataGrid has been moved to its new page, the control rebinds the DataGrid and calls the BuildPagerButtons
() method, which rebuilds the paging links to match the grid's updated page position.
As you've seen in this article, you can use a simple server control to add search-engine-style paging to a DataGrid. I've purposely kept the example control as simple as possible to make it easy to understand. You may choose to extend the control by adding additional properties, functionality, designer support, or other items. I hope you found this control handy and that it (or some variation of it) works its way into your development efforts. I know I'll be carrying it in my bag of tricks for some time to come.