Custom ASP.NET Datagrid Paging With Exact Count, Part 2
By Dimitrios Markatos
In Part 1 we looked at creating a custom paging solution using
ASP.NET's DataGrid Web control. In Part 1 we focused in on the first
(and most detailed) server-side subroutine: BindSQL(). In this part we'll look at the HTML
content of the custom paging Web page and examine the other server-side subroutines and event handlers.
The HTML Portion
In our HTML section we need a couple of label controls to display various bits of information, such as
what page we are currently viewing, how many total records there are, etc. We also need a DataGrid, to which
we are binding the database results to. Don't forget to put the DataGrid in a WebForm (a <form>
tag with the runat="server" attribute specified). Finally, we need a series of LinkButton Web controls,
to display our Prev/First Page and Next/Last Page links.
The content for the HTML section is as follows:
|
Note that in our DataGrid Web control we set the PagerStyle's Visible property
to False. This is because we are implementing our own paging solution, and don't want to use the default
paging style supported by the DataGrid Web control. (For more information on paging database results using
the DataGrid's built-in functionality, be sure to read: Paging Database Results
in ASP.NET!) Also note that the four LinkButton controls all specify the server-side subroutine
PagerButtonClick as the sub to be called when they are clicked; similarly, the "Change Pagesize"
button has the RePage subroutine defined as its OnClick event handler.
The event handler for the four LinkButtons (PagerButtonClick) must display the appropriate page
of data, be it the next page, the previous page, the first page, or the last page. Which page to display, of
course, depends on what LinkButton the user clicked. The PagerButtonClick (shown below) uses the
CommandArgument passed in from the LinkButton Web controls to determine which control was clicked,
and then takes the appropriate action.
|
The RePage event handler, which is called when the "Change Pagesize" button is clicked, simply
resets the DataGrid's CurrentPageIndex property back to 0 and rebinds the database data:
|
Finally, the last two server-side subroutines are two meager helper subroutines, Next_Buttons()
and Prev_Buttons(), which display the correct text for each of the LinkButtons. These two
subs, which are called from BindSQL(), can be seen below:
|
Well, that's it! Be sure to view the complete code, try out the live demo, and read up on the related articles! If you have any questions, please do not hesitate to email me!
Happy Programming!
Attachments:




