If you are a developer and like me you mostly develop data driven applications, then I am sure that you have had to write a couple of paging functions and controls. I very rarely use the asp.net datagrid control as I find it bloated and harder to customize its UI. Instead I use the handy and much lighter weigth datalist and repeater controls.
Of course the drawback with the afore mentioned controls is that they do not come equipped with an integrated paging control as the datagrid does, but the folks at DotNetNuke did a fine job at remedying the situation: they built a re-usable Paging Control. If you are a DotNetNuke module developer, you should definitely consider using it instead of building your own. This is exactly what I did, and I am now sharing my experience since finding these "nuggets" of useful built-in functions and controls within DotNetNuke is more like "treasure hunting"!
First and foremost, I have to note that the DotNetNuke paging control does not manage the data, in fact it does not even bind to a source. All that the DotNetNuke Paging Control does is generate a paging interface, and builds the paging links based on some key data that you feed into it at run time. The information you provide control at runtime is as follows:
Once you have filled your collection object, get its row count and feed it to this property of the control
Define the size of each page (i.e. the number of records you wish to display on each page)
Get the current page index and feed it into this property (i.e. Once the pager initiates the paging, you can get the pageindex from the querystring called CurrentPage)
If your module needs its own custom querystring parameters, feed them into this property, the paging links will append them for you so that your data continues to load normally)
The following is a code example for filling the the properties mentioned above:
ctlPagingControl.TotalRecords = TotalRecords ctlPagingControl.PageSize = PageSize ctlPagingControl.CurrentPage = PageIndex
The following is a screen shot of the generated Paging Control:
Putting it all together
Let’s start by adding the control to your UI page. First we must register the WebControl assembly (DotNetNuke.UI.WebControls) at the top of your page, and then add the control to a location of the page of your choice. The code example below assumes you are adding this paging control below your datalist/repeater:
<%@ Register TagPrefix="dnn" Namespace="DotNetNuke.UI.WebControls" Assembly="DotNetNuke" %> <asp:Datalist ID="Datalist1" runat="server"></asp:Datalist> <dnn:PagingControl id="PagingControl1" runat="server"></dnn:PagingControl>
The next (and last) thing to do is stitch the control to your data source.
First let me explain that there are two ways (that I know of) you can page your data:
- Via Stored Procedure or Data Access:
This is the most labor intensive, but will give you better performance. Basically you page the data in the stored procedure itself by passing the PageIndex and the PageSize into it so that it return and fills your collection with just the rows that you need.
- Via Use the PagedDataSource Class
This class enables you to page the entire source with much less code, however, all the rows are fetched at each page request, regardless of which page you are on (Unless you do some additional data caching in your asp.net code, in which case you are ok).
For simplicity’s sake, the example I provide below implements this methodology.
Private Sub BindDatalist() Dim PageSize = 20 'Display 20 items per page 'Get the currentpage index from the url parameter If Request.QueryString("currentpage") IsNot Nothing Then _CurrentPage = Request.QueryString("currentpage").ToString Else _CurrentPage = 1 End If 'Call you data layer and get the data you need to bind to the datalist Dim objDataController As New YouDataController Dim objCollection As List(Of YourObject) = objDataController.GetData() 'Fill the PagedDataSource object's datasource property with your collection Dim objPagedDataSource As New PagedDataSource objPagedDataSource.DataSource = objCollection If Me.PageSize > 0 Then objPagedDataSource.PageSize = PageSize objPagedDataSource.CurrentPageIndex = _CurrentPage - 1 objPagedDataSource.AllowPaging = True End If 'Bind the datalist dlWhoIsOnline.DataSource = objPagedDataSource dlWhoIsOnline.DataBind() 'Show/hide the pager based on on the number of rows fetched vs pagesize If PageSize = 0 OrElse objDataController.Count <= PageSize Then ctlPagingControlBottom.Visible = False Else ctlPagingControlBottom.Visible = True With ctlPagingControlBottom .TotalRecords = objDataController.Count .PageSize = PageSize .CurrentPage = _CurrentPage .TabID = TabId End With End If End Sub
You should now be able to run your project and test the paging control.
Feel free to post your comments below if you need additional information.