Print Page | Close Window

Paging for DataList

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=13508
Printed Date: 28 March 2026 at 9:09pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Paging for DataList
Posted By: Misty
Subject: Paging for DataList
Date Posted: 26 January 2005 at 1:28am
I need some help with the code for a datalist. I want to use paging for this datalist.
 
I got the following error message: Compiler Error Message: BC30456: 'CurrentPageIndex' is not a member of 'System.Web.UI.WebControls.DataList'.

Source Error:

Line 25:          
Line 26:                  'Get new paging index 
Line 27:                  dtlGBook.CurrentPageIndex = objArgs.NewPageIndex 
 
I know what this means. CurrentPageIndex is not a member of DataList. It's actually a member of DataGrid. What should I use instead for DataList?
 
Let me give the significiant parts of my code:
 
 
 Sub dtlGBook_PageChanger(objSender As Object, _ 
                & ;nbs p;                &a mp;n bsp;           objArgs As DataGridPageChangedEventArgs )
        
                 'Get new paging index
                 dtlGBook.CurrentPageIndex = objArgs.NewPageIndex

        
                 Call BindDataList()
        
        End Sub
        '---------------------------------------------
        ' name: BindDataList()
        '---------------------------------------------
        Sub BindDataList()
                 Dim strConnect As String
                 Dim objConnect As New OleDbConnection()
                 Dim objCommand As New OleDbCommand()
                 Dim strSQL as String
                 Dim dtaGBook As New OleDbDataAdapter()
                 Dim dtsGBook As New DataSet()
            Dim databaseName as String
            Dim path as String
        databaseName = ""
       
        'Create Connection object 
                
         
                 objConnect.ConnectionString = strConnect
                 objConnect.Open()
     
        'Build SQL string
        strSQL = "My SQL Statement"
 
                 'Set the Command Object properties
                 objCommand.Connection = objConnect
                 objCommand.CommandType = CommandType.Text
                 objCommand.CommandText = strSQL
 
                 'Create a new DataAdapter object
                 dtaGBook.SelectCommand = objCommand
 
                 'Get the data from the database and
                 'put it into a DataTable object named dttGBook in the DataSet object
                 dtaGBook.Fill(dtsGBook, "dttGBook")
 
                 'Set the DataSource property of the DataList 
                 dtlGBook.DataSource = dtsGBook
 
                 'Bind all the controls on the page
                 dtlGBook.DataBind()
        End Sub
 
Small parts of my code after <form runat="server"> :
   <asp:datalist id="dtlGBook" runat="server"
        width=600   
        AllowPaging="True"
         OnPageIndexChanged="dtlGBook_PageChanger"
         PagerStyle-Mode="NumericPages"
         Pagesize="5"
     
        enableviewstate=false>
 



Replies:
Posted By: Mart
Date Posted: 26 January 2005 at 2:21am
DataLists don't actually have built in paging, you have to implement it yourself. This may help you:


'For this function you just need to add a panel called pnlPager that will contain the pages, you also need to change the URL's that are built below

    Public Function GetPagedDataSource(ByVal DataSource As IEnumerable) As PagedDataSource
        Dim pds As New PagedDataSource

        pds.DataSource = DataSource
        pds.AllowPaging = True
        pds.PageSize = CInt(ddlItemsPerPage.SelectedItem.Value)
        Dim CPage As Integer = CInt(Request.QueryString("Page"))
        pds.CurrentPageIndex = CPage

        For i As Integer = 1 To pds.PageCount
            Dim pageLink As Object
            Dim spacer As New LiteralControl(" ")

            If (i - 1) = pds.CurrentPageIndex Then
                pageLink = New Label
            Else
                pageLink = New HyperLink
                If Request.QueryString("q") Is Nothing Then
                &n bsp;   pageLink.NavigateUrl = "default.aspx?FeedId=" & CInt(Request.QueryString("FeedId")) & "&Page=" & (i - 1)
                Else
                &n bsp;   pageLink.NavigateUrl = "default.aspx?q=" & Request.QueryString("q") & "&Page=" & (i - 1)
                End If
            End If

            pageLink.Text = i

            pnlPager.Controls.Add(pageLink)
            pnlPager.Controls.Add(spacer)
        Next

        Return pds
    End Function

'Then just

YourRepeater.DataSoruce = GetPagedDataSource(YourDataSet.Tables(0).DefaultView)


This code does work, as I wrote it for http://www.devjunkies.com/news - http://www.devjunkies.com/news


Posted By: Misty
Date Posted: 27 January 2005 at 1:09am
Mart,
 
I tried finding the article that you wrote for http://www.devjunkies.com/news - http://www.devjunkies.com/news , but I could not find it. Can you please give me the exact URL?
 
I had a couple of problems when I tried to use your code.
 
1.) I got the following error message regarding pds.PageSize = CInt(ddlItemsPerPage.SelectedItem.Value) : Compiler Error Message: BC30451: Name 'ddlItemsPerPage' is not declared.

Source Error:

Line 24:         pds.DataSource = DataSource
Line 25:         pds.AllowPaging = True
Line 26:         pds.PageSize = CInt(ddlItemsPerPage.SelectedItem.Value)
 
2.) I added this line: dim ddlItemsPerPage, but I got the following error message: Exception Details: System.NullReferenceException: Object variable or With block variable not set.

Source Error:

Line 24:         pds.DataSource = DataSource
Line 25:         pds.AllowPaging = True
Line 26:         pds.PageSize = CInt(ddlItemsPerPage.SelectedItem.Value)
 
 3.) The same thing happened with pnlPager.


Posted By: Mart
Date Posted: 27 January 2005 at 2:15am
I meant I used that code when building www.devjunkies.com/news so it does work.

You need to change line 26 from

       pds.PageSize = CInt(ddlItemsPerPage.SelectedItem.Value)

to something like

       pds.PageSize = 10
Depending on how many items you want to be shown on each page


Posted By: Misty
Date Posted: 27 January 2005 at 2:25am

That solved the problem with the PageSize. I am still having problems with pnlPager. I tried doing this: dim pnlPager. It would not work. How do I solve this problem? You can find the error message that I got below.

Compiler Error Message: BC30451: Name 'pnlPager' is not declared.

Source Error:

Line 48:             pageLink.Text = i
Line 49: 
Line 50:             pnlPager.Controls.Add(pageLink)
Line 51:             pnlPager.Controls.Add(spacer)


Posted By: michael
Date Posted: 27 January 2005 at 9:42am
pnlPager is a Panel Control that Mart added below the datalist, in that panel the pager controls are located.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Mart
Date Posted: 27 January 2005 at 11:13am
You need to follow the instructions I put in the comment before the function:

'For this function you just need to add a panel called pnlPager that
will contain the pages, you also need to change the URL's that are used in the function



Posted By: Misty
Date Posted: 27 January 2005 at 1:00pm
I had already changed the name of the web pages from default.aspx to my own web page.
 
I added the following line of code: <asp:label id="pnlPager" runat="server" Font-Names="Verdana" Font-Size="12px"/>  at the bottom of the DataList. This solved the problem. Thank you for sharing the code with me!


Posted By: Misty
Date Posted: 03 February 2005 at 1:30am
I know how to do paging for a data list now. However, I would like to know how you can add CSS style to <asp:label id="pnlPager" runat="server" Font-Names="Verdana" Font-Size="12px"/>.
 
Here's an example of a hyperlink that uses CSS:

<a class=TOC HREF="sign.aspx">Sign Guest Book</A>

Can someone please tell me how to apply a CSS style to a label like pnlPager?



Posted By: Mart
Date Posted: 03 February 2005 at 2:13am
<asp:label id="pnlPager" runat="server" CssClass="TOC" />


Posted By: Misty
Date Posted: 04 February 2005 at 1:54am
Originally posted by Mart Mart wrote:

<asp:label id="pnlPager" runat="server" CssClass="TOC" />
 
Unfortunately, this wouldn't work. I also tried removing Css.
 
Here's my CSS code:
 
<STYLE TYPE="text/css">
<!--
 .MyH1 
 { 
  font-family:small arial,sans-serif;    
  font size: 15;
  color: Navy;
  font-weight: bold;
 }
 
 a.Navigate
 {
   
     font:small arial,sans-serif
     font size: 15;
  color: Navy;  
  text-decoration:underline;
  font-weight:bold;  
 }
 
 a.TOC
 {
  font-family:small arial,sans-serif
  font size: 15;
  font-style:Bold;
  color:Navy; 
  text-decoration:none;
  font-weight: bold;
 }
 a.TOC:hover
 {
  color: Navy;
  font size: 15;
  font-style:Bold;
  text-decoration:underline; 
  font-weight: bold; 
 }
-->
</STYLE> 
 
How do I get the page numbers to be in CSS style?


Posted By: Leeb65
Date Posted: 10 February 2005 at 2:26am
instead of:
 
a.TOC
 {
  font-family:small arial,sans-serif
  font size: 15;
  font-style:Bold;
  color:Navy; 
  text-decoration:none;
  font-weight: bold;
 }
 
try:
 
.TOC
 {
  font-family:small arial,sans-serif
  font size: 15;
  font-style:Bold;
  color:Navy; 
  text-decoration:none;
  font-weight: bold;
 }



-------------
Lee

http://www.rheinsitemedia.de">



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net