Article

Paging Results with ASP.NET's PagedDataSource

Page: 1 2 3 4 5

Almost there! Our last call of the day is creating the Web Form to display the page:

<form runat="server">    
  <font size="-1" face="Verdana, Arial, Helvetica, sans-    
serif"><asp:label id="pageNumber" runat="server" /></font>    
  <asp:DataList id="theDataList" runat="server">    
     <ItemTemplate>    
        <table border="0" cellpadding="0" cellspacing="0"      
width="500">    
           <tr>    
              <td width="140"><font size="-1" face="Verdana, Arial,      
Helvetica, sans-serif"><strong>Company Name</strong>:</font></td>    
              <td><font size="-1" face="Verdana, Arial, Helvetica,      
sans-serif"><%# DataBinder.Eval(Container.DataItem, "companyName")      
%></font></td>    
           </tr>    
           <tr>    
              <td width="110"><font size="-1" face="Verdana, Arial,    
Helvetica, sans-serif"><strong>Contact Name</strong>:</font></td>    
              <td><font size="-1" face="Verdana, Arial, Helvetica,      
sans-serif"><%# DataBinder.Eval(Container.DataItem, "contactName")      
%></td>    
           </tr>    
           <tr>    
              <td width="110"><font size="-1" face="Verdana, Arial,      
Helvetica, sans-serif"><strong>Contact Title</strong>:</font></td>    
              <td><font size="-1" face="Verdana, Arial, Helvetica,    
sans-serif"><%# DataBinder.Eval(Container.DataItem, "contactTitle")      
%></font></td>    
           </tr>    
        </table>    
     </ItemTemplate>    
     <separatortemplate>    
        <hr color="#0099FF" />    
     </separatortemplate>    
  </asp:DataList>    
   
  <asp:LinkButton id="btnPrev" Text="&lt;" OnClick="Prev_Click"      
runat="server" />    
  <asp:LinkButton id="btnNext" Text="&gt;" OnClick="Next_Click"      
runat="server" />    
</form>

And that's it! We keep the Page_Load sub routine and the line to declare the PagedDataSource object the same as before:

Dim pagedData As New PagedDataSource    
   
Sub Page_Load(byVal obj As Object, byVal e As EventArgs)    
  doPaging()    
End Sub

It's that simple to add paging functionality to a DataList. If you wanted the above to be a Repeater list, all you'd have to do would be to replace the DataList tags with Repeater.

Something Like Homework

As I said before, the PagedDataSource also works with RadioButton, CheckBox and DropDown lists. Try it out -- I'm not 100% sure what actual functionality that will offer, but it's handy to know.

There's a whole host of things you can do with this object:

  • have page numbers as well as the Previous/Next buttons,
  • have a Previous/Next 10 button or
  • have a jump to the end button.

All these tasks can be easily accomplished using the PagedDataSource object.

You can also download all the code used in this article in a handy zip. The examples are in three languages: VB.NET, C# and Jscript.NET. So there's something for everyone!

Signing Off

The .NET Framework is packed full of classes like these, that are aimed at making your life easier -- it's just a matter of finding and working with them. For this, I use the .NET Framework SDK Documentation. When you have some spare time, have a browse through and see what other useful classes you can find!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: