So, is this difficult? Not at all, certainly not much more involved than enabling the standard paging on the DataGrid, and it is so much better. The following two figures show what it takes to declare and use the DataGridPager in your pages.
Figure 2 – Registering the DataGridPager
<%@ Register TagPrefix="lchip" Namespace="Littlechip.Web.UI.WebControls"
Assembly="Littlechip.DataGridPager" %>
If you put the Littlechip.DataGridPager.dll in your application’s bin directory, it should find it using this control registration directive. Then you can use it anywhere on the page as in Figure 3.
Figure 3 – Declaring a DataGridPager
<LCHIP:DataGridPager id="TopPager" runat="server" PagerGrid="Orders"
ItemCountStyle="Bottom" Width="500px" CssClass="Pagers"
UseConfigSettings="True"></LCHIP:DataGridPager>
As you can see, there’s not much to this, and all of the properties set here can be set in Design View in Visual Studio .NET using the Property Grid. The PagerGrid specifies the ID of the DataGrid that the pager will be hooked up to—the Property Grid will even provide a drop-down list of all the DataGrids on the page. ItemCountStyle is an enumeration that you can use to specify where to display the item count summary. Width and CssClass are obvious, and UseConfigSettings allows you to set some application-wide DataGridPager settings in the web.config file.
Unfortunately, that’s not all there is to it. ASP.NET 1.1 requires code to do a lot of simple things, and my control is no exception (maybe it’ll be better if I update it for the GridView!). But for now, we still need to do a bit of code. The nice thing is that, if you’re using Visual Studio .NET, you can automate the event wiring by double-clicking on the PageIndexChanged event on the Property Grid event list. Figure 4 shows the code that you’d need to write in C# to do this manually.
Figure 4 – Wiring Up the DataGridPager’s PageIndexChanged Event
this.TopPager.PageIndexChanged += new
System.Web.UI.WebControls.DataGridPageChangedEventHandler(
this.Pager_PageIndexChanged);
<p class="MsoNormal">
private void Pager_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.BindGrid();
}
The first line in Figure 4 actually belongs in the OnInit method (or InitializeComponent if Visual Studio .NET does it for you). The Pager_PageIndexChanged method matches the System.Web.UI.WebControls.DataGridPageChangedEventHandler delegate, so it should be familiar if you’ve done DataGrid paging before. In fact, you’d pretty much have to do exactly this if you were to use the built-in paging. The nice thing is that, as you might note, all I do is call the BindGrid method; the DataGridPager will automatically reset the DataGrid’s page index for you if you’re not using custom paging.
Note that “custom paging” here means that you are getting individual pages from your data source as opposed to loading up your entire result set. In our example, we don’t use custom paging, but the DataGridPager does work with it—you just have to be sure to set the DataGrid’s VirtualItemCount property.
The only other thing you need to do to get paging going is to enable paging on your DataGrid just like you normally would if you weren’t using the DataGridPager—by setting its AllowPaging property to True. But unlike that, you need to also set the PagerStyle’s Visible property to False as in Figure 5.
Figure 5 – Hiding DataGrid’s Built-In Paging
<PagerStyle Visible="False" />
At this point, you should be good to go. If you run your page, it should display the pager that you set up and process page change commands by either the first, previous, next, last or direct page access links. Of course, you’ll probably also want to customize the look and feel a bit to make it prettier. The easiest way to do this, in my opinion, is by setting the CssClass property of the pager, which is also an easy way to ensure visual consistency amongst your pagers.
The last thing I’ll point out is the way you can set up the pager’s web.config settings. Figure 6 shows what you’ll need to add in your web.config file.
Figure 6 – Setting up web.config for UseConfigSettings
<configSections>
<section name="PagerSettings"
type="System.Configuration.NameValueFileSectionHandler, System,
Version=1.1.4322.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<PagerSettings>
<add key="PriorPageCount" value="5" />
<add key="AddPageCount" value="5" />
</PagerSettings>
The properties that you can set in this manner are FirstButtonText, PreviousButtonText, NextButtonText, LastButtonText, PriorPageCount, AddPageCount, DisplayPageNumbers, ItemCountStyle, DisplayFirstLast, DisplayPreviousNext, and ItemSummaryText. As you can see, you use the property name as the key in the PagerSettings section and specify the value you want.