Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Add Search-Engine-Style Paging To Your DataGrids : Page 3

You don't have to just accept the default Microsoft DataGrid paging scheme. Find out how to create a server control that attaches to your existing DataGrids and provides paging like that found on search-engine results pages.


advertisement
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() End Sub

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 property.

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() and PagerLinkButtonClick() methods.

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 and PreviousPageLinkText) that you can use to specify the text for these controls. Also note that the code sets each LinkButton's CommandName and CommandArgument 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 and CommandArgument 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.



Rusty Speidel is a consultant with inLogic, which builds custom business solutions in the Atlanta, Georgia area. He's an MCSD and develops software using technologies such as VB.NET, ASP.NET, SQL Server, XML/XSL/XSLT/XPath, HTML/DHTML, Macromedia Flash, and others.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date