What's New at CTO 2.0 ...

Feb 13

Written by: Antonio Chagoury
Wednesday, February 13, 2008 

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:

  • TotalRecords
    Once you have filled your collection object, get its row count and feed it to this property of the control
  • PageSize
    Define the size of each page (i.e. the number of records you wish to display on each page)
  • CurrentPage
    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)
  • Querystring
    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:

DNNPaginControl

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:

  1. 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.
  2. 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.

Tags:

25 comment(s) so far...

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Great idea to share some of these gems hidden inside DotNetNuke. Unless you've got the time to sit and read through the source code, it's hard to find some of these hidden features. Thanks so much for sharing this with the community!

By Don Worthley on   Wednesday, February 13, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

@Don Worthley: I wish I could do even more "sharing" to be honest - those darn bills keep getting in the way though! :)

By antoniochagoury on   Wednesday, February 13, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Very nice write up, thanks!

By Matt Christenson on   Thursday, February 14, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

THANK YOU ! I'll definitely be looking forward to more as I didn't know this gem existed!

By Andrew Walker on   Monday, February 18, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Hello, I'm new to DNN and you just answered 1/2 of my problem. The other half is how to turn the paging control into a browsing control, and more importantly (for SEO purposes), to have it show each page as a distinct URL component. The initial reading of the documentation does not point to any simple solutions. Any help would be appreciated.

By Aldo Salzberg on   Tuesday, February 26, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Aldo, the Paging Control triggers the paging via QueryString (in the URL as opposed to postbacks). In theory this is SEO friendly as any BOT can index the pages by simply navigating the URLs. Each page is also theoretically a distinct URL given that the "CurrentPage" parameter is different for each page (i.e. ...¤tpage=123).

I am not sure if this answers your question, if not, please me know what exactly you are trying to accomplish.

Enjoy!

By antoniochagoury on   Tuesday, February 26, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Hi, Antonio. Thanks for the reply and the clarification. It seems, however, that the paging control is no longer part of DotNetNuke.UI.WebControls dll. I've looked at a few previous versions with no luck. Could you clarify where this control is located? Thanks! Aldo

By Aldo Salzberg on   Wednesday, February 27, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

@Aldo: I am quite sure it is still there... I just used it ;)
If you are looking for an ascx, then you will not find it. This is a Web Control.

In your UI file, just register the WebControls:

#%@ Register TagPrefix="dnn" Namespace="DotNetNuke.UI.WebControls" Assembly="DotNetNuke" %#

... and then just add the control as follows:

#dnn:PagingControl id="PagingControl1" runat="server">

Let me know how that works out for you.

(Please note that you have to replace the # with < and > tags, as the comment box will not allow HTML)

By antoniochagoury on   Wednesday, February 27, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Great writeup on this! I was looking to write something about this myself, but just haven't had the time.

One thing that should be noted here is that you REALLY should do more data validation on the CurrentPage querystring value. Without validation it would be very easy for users to manipulate the value to cause a number of errors.

By Antonio on   Wednesday, February 27, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Thank you for your help. I was looking in the wrong dll!

By Aldo Salzberg on   Friday, February 29, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Would I be able to change the UI of the Paging Control with a text editor? Which file would this be in? The Feedback module uses First, Previous, Next, Last controls, which make it too long for the small column I'm putting it in. If I could change to arrows (»), this would be great.

By Ken on   Sunday, March 23, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Ken,

You can change the text found in the control from the Language Editor.
In order to change the UI, however, you will need to modify code in the actual control code found in the core.

I hope this helps.

By antoniochagoury on   Sunday, March 23, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Yes, that is exactly what I needed. Thanks

By Ken on   Tuesday, March 25, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Outstanding resource & well explained. However, I can't get mine to work, and I'm sure the reason should be obvious.

Navigate to http://prorec.dynalias.net/ where you'll see I've added the PagingControl to my BlogDisplay module. The control is creating the right links, however, regardless of what you click on, you end up back at the original URL.

Interestingly, if I manually create the URL http://prorec.dynalias.net/default.aspx?currentpage=2 it works correctly.

By Rip Rowan on   Monday, May 05, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Even more curious, the "hacked URL" only works when I'm logged in... otherwise, it continues to display the first set of blog entries.

By Rip Rowan on   Monday, May 05, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

@Rip: From looking at your site, it looks like you are not specifying the TABID of the pager control. Make sure you always specify it like this:

ctlPagingControl.TabID = TabId

Where TabId is the TABID in context provided by the ModuleControlBase class.

;-)

By antoniochagoury on   Monday, May 05, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Thank you Antonio. This is what I didn't found googling

By dnnR3xha on   Tuesday, July 01, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Would it be possible to alter the pager control to use IModuleCommunicator instead of URLs?
I'm trying to avoid postbacks.

By Kristian on   Tuesday, July 15, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

How can I get rid of the border. I tried to set the BorderStyle=None and BorderStyle=NotSet and neither works.

By Edward on   Thursday, August 21, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

How can I get rid of the border. I tried to set the BorderStyle=None and BorderStyle=NotSet and neither works.

By Edward on   Thursday, August 21, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

i would like to add the pagecontrol to my dnn blog:
1) do i put this code in ViewBlog.ascx?






2) where do i put the "Private Sub BindDatalist()"?

By raf on   Saturday, November 15, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

i would like to add the pagecontrol to my dnn blog:
1) do i put the registration code (Register TagPrefix...) in ViewBlog.ascx?
2) where do i put the "Private Sub BindDatalist()"?

By raf on   Saturday, November 15, 2008

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Thank you Antonio,

i have One issue
i Drop to Same Module In on Tab Ok One Module Showing Records Vertically And Another Module Showing Horizontal ok
if click Next Link of Paging Control i need to show only First module Next Records only But this Control Showing Next records Of Second Module also

Please send me any solution

By nizamuddin on   Monday, January 05, 2009

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

@nizamuddin: You should be able to append an additional query string parameter to paging links, such as a controlId, example (a poor man's "sender as object" if you will). Then in your Binding event, check for the "sender" and if the controlId matches your criteria then, and only then, you page the control.

HTH.

By Antonio Chagoury on   Monday, January 05, 2009

Re: DNN Best Kept Secrets Part 1: DNN Paging Control

Thank you Antonio,

i have One issue
i Drop to Same Module In on Tab Ok One Module Showing Records Vertically And Another Module Showing Horizontal ok
if click Next Link of Paging Control i need to show only First module Next Records only But this Control Showing Next records Of Second Module also

Please send me any solution

By nizamuddin on   Monday, January 05, 2009

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 

RSS Snapshot

Antonio Chagoury - Microsoft MVPAntonio Chagoury
Software Architect, Microsoft MVP, Open Source Advocate, Golfer, Cigar Afficionado
Washington DC - USA
Company: Inspector IT, Inc.
Profiles: LinkedIn &

Antonio Chagoury - Microsoft MVP

RSS Read it in your RSS reader

 
Add to Google Reader Add to Bloglines Add to My Yahoo Add to Netvibes
 

Tag Cloud

2008   add   blog   code   community   control   data   dnn   dotnetnuke   get   google   great   group   just   know   live   may   microsoft   module   net   new   page   post   presentation   see   sharepoint   support   sure   time   use   user   way   web   windows   work  

 

    Archives

    Blog Roll