Ask the DotNetJunkies: DataList Paging
By eric neff mcp
Published: 3/24/2001
Reader Level: Beginner
Rated: This article has not yet been rated.
Be the first to rate it!
Tell a Friend
Rate this Article
Printable Version
Discuss in the Forums

Question:

Could you teach me how to use paging to "DataList" web control like "DataGrid",just like [ASP.NET News] or [ASP.NET Tutorials] on your site? I hope you can help me with this question or kindly send source code (news.aspx) to me. Thank you very much!! A Rookie in ASP.NET.

Charles Wong

Answer:

The trick when using a DataList to page data is to think about where the paging occurs. Since the DataList does not support paging but does support being bound to a DataSet. If it were simple the story would end here. Unfortunately a DataSet does not support paging either. So what you need to do is make your DataSet a single page of data! This way you only retrieve the data you need for a single page.

So you need to implement a custom paging solution. Fortunately a custom paging solution already exists for you. If you consult the excellent article "ASP.NET DataGrid Paging Part 2 - Custom Paging" you will find a solution for binding a paged DataSet to a DataGrid. Converting it to a DataList is simple. In the article replace the DataGrid code here:

<asp:DataGrid runat="server" id="myDataGrid"
   Width="740"
   Cellpadding="4"
   Cellspacing="0"
   Gridlines="Horizontal"
   HorizontalAlign="Center"
   HeaderStyle-CssClass="tableHeader"
   ItemStyle-CssClass="tableItem"
   AlternatingItemStyle-BackColor="#FFFFCC"
   AllowPaging="True"
   AllowCustomPaging="True"
   PageSize="10"
   PagerStyle-Visible="False"
/>

With the DataList code here:

<asp:DataList Runat="server" id="myDataGrid"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="4"
CellSpacing="0">
   <template name="itemtemplate">
    <%# Container.DataItem("CompanyName") %>
    </td><td>
    <%# Container.DataItem("ContactName") %>
    </td><td>
    <%# Container.DataItem("ContactTitle") %>
    </td><td>
    <%# Container.DataItem("Phone") %>
    </td><td>
    <%# Container.DataItem("Fax") %>
   </template>
</asp:DataList>

Add this line of code in the BindData() method:

dim PageSize as Integer = 10

Change this line:

.Parameters("@PageSize").Value = myDataGrid.PageSize

to this:

.Parameters("@PageSize").Value = PageSize

and this line:

TotalPages.Text = System.Math.Ceil(myDataSetCommand.SelectCommand.Parameters("@TotalRecs").Value/myDataGrid.PageSize)

to this:

TotalPages.Text = System.Math.Ceil(myDataSetCommand.SelectCommand.Parameters("@TotalRecs").Value/PageSize)

You are done.

Don't forget to add the stored procedure to your database.

[Ed Note:] The News and Tutorials pages of this Web site both use the DataGrid and custom ItemTemplates. The DataGrid is rather flexible in its output. See "Working with DataGrid Templates" for a tutorial on using the DataGrid for custom layout.



Marketplace
(Sponsored Links)
What are the green links?
   



 
Copyright © 2007 CMP Tech LLC |
Privacy Policy (4/10/06) | Your California Privacy Rights (4/10/06) | Terms of Service | Advertising Info | About Us | Help