Article

Paging Results with ASP.NET's PagedDataSource

Page: 1 2 3 4 5 Next

The DataList

So we've created our PagedDataSource instance and our data source. Now, let's make a DataList to display it!

<asp:DataList id="theDataList" runat="server">  
 <ItemTemplate>  
   <hr size="0" />  
   Department: <%# DataBinder.Eval(Container.DataItem,  
   "Key") %><br />  
   Employee ID: <%# DataBinder.Eval(Container.DataItem,  
   "Value") %></a><br />  
 </ItemTemplate>  
</asp:DataList>

That's it! As you can see, we've created a simple DataList.

Putting it Together

Now, let's put the code and DataList together to create the actual page:

<%@ Page Language="VB" %>  
<script language="VB" runat="server">  
Sub Page_Load(byVal obj As Object, byVal e As EventArgs)  
  doPaging()  
End Sub  
 
Sub doPaging()  
     Dim mockData As New HashTable()  
  Dim IDx As Integer = 0  
  Do Until IDx = 500  
     mockData.Add(IDx.toString(), (IDx * 101).toString)  
     IDx += 1  
  Loop  
 
  Dim pagedData As New PagedDataSource()  
  pagedData.DataSource = mockData  
  pagedData.AllowPaging = True  
  pagedData.PageSize = 5  
 
  theDataList.DataSource = pagedData  
  theDataList.DataBind()  
End Sub  
</script>  
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
     <title>Paging Example 1</title>  
  </head>  
 
  <body>  
     <asp:DataList id="theDataList" runat="server">  
        <ItemTemplate>  
           <hr size="0" />  
           Department: <%# DataBinder.Eval(Container.DataItem,  
           "Key") %><br />  
           Employee ID: <%# DataBinder.Eval(Container.DataItem,  
           "Value") %></a><br />  
        </ItemTemplate>  
     </asp:DataList>  
  </body>  
</html>

See what happens? When the page is loaded, the Page_Load sub-routine is run, which then checks to see if the page has been posted back. If it hasn't, it then runs the doPaging() sub-routine.

What does our page look like? Run it and see! Here's the result you can expect:

921_Result1

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

Sponsored Links