Connecting Tech Pros Worldwide Help | Site Map

need paging in numbers till last page from db in datalist..??

Newbie
 
Join Date: Jan 2009
Posts: 23
#1: May 7 '09
The below workss very fine , in the type for previous, next concept for paging,

i need to do with page number Like this,

1 2 3 4......23 (http://bytes.com/topic/visual-basic-net/) Like this paging in our site........

Till 4 page visible and then last page number,,,,,? Experts plz suggest ..thx...?


Expand|Select|Wrap|Line Numbers
  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3.  
  4. Partial Public Class pagingtest
  5.     Inherits System.Web.UI.Page
  6.     Private pagedData As New PagedDataSource()
  7.     Private CurPage As Integer = 1
  8.  
  9.     Private Sub Page_Load(ByVal obj As [Object], ByVal e As EventArgs) Handles Me.Load
  10.         doPaging()
  11.     End Sub
  12.  
  13.     Public Function getTheData() As DataTable
  14.  
  15.         Dim DS As New DataSet()
  16.         Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
  17.  
  18.         Dim objSQLAdapter As New SqlDataAdapter("SELECT CompanyName, ContactName, ContactTitle FROM Customers", myConnection)
  19.         objSQLAdapter.Fill(DS, "Customers")
  20.         Return (DS.Tables(0))
  21.  
  22.  
  23.     End Function
  24.  
  25.     Private Sub doPaging()
  26.  
  27.         pagedData.DataSource = getTheData().DefaultView
  28.  
  29.         pagedData.AllowPaging = True
  30.         pagedData.PageSize = 4
  31.         Try
  32.             If Request("Page").ToString() IsNot Nothing Then
  33.                 CurPage = Int32.Parse(Request("Page").ToString())
  34.             Else
  35.                 CurPage = 1
  36.             End If
  37.  
  38.             pagedData.CurrentPageIndex = CurPage - 1
  39.         Catch ex As Exception
  40.  
  41.             pagedData.CurrentPageIndex = 0
  42.         End Try
  43.  
  44.         btnPrev.Enabled = (Not pagedData.IsFirstPage)
  45.         'btnPrev.Visible = (!pagedData.IsFirstPage);
  46.  
  47.         btnFirst.Enabled = (Not pagedData.IsFirstPage)
  48.  
  49.         btnNext.Enabled = (Not pagedData.IsLastPage)
  50.         'btnNext.Visible = (!pagedData.IsLastPage);
  51.  
  52.         btnLast.Enabled = (Not pagedData.IsLastPage)
  53.  
  54.         'pagedData.CurrentPageIndex = CurPage - 1;
  55.  
  56.         lblCurrentPage.Text = ("Page: " & CurPage.ToString() & " of ") & pagedData.PageCount.ToString()
  57.  
  58.         theDataList.DataSource = pagedData
  59.  
  60.         theDataList.DataBind()
  61.     End Sub
  62.  
  63.     Protected Sub btnPrev_Click(ByVal sender As Object, ByVal e As EventArgs)
  64.         Response.Redirect((Request.CurrentExecutionFilePath & "?Page=") & (CurPage - 1))
  65.     End Sub
  66.  
  67.     Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs)
  68.         Response.Redirect((Request.CurrentExecutionFilePath & "?Page=") & (CurPage + 1))
  69.     End Sub
  70.  
  71.     Protected Sub btnFirst_Click(ByVal sender As Object, ByVal e As EventArgs)
  72.         Response.Redirect((Request.CurrentExecutionFilePath & "?Page=") & (1))
  73.     End Sub
  74.  
  75.     Protected Sub btnLast_Click(ByVal sender As Object, ByVal e As EventArgs)
  76.         Response.Redirect((Request.CurrentExecutionFilePath & "?Page=") & (pagedData.PageCount))
  77.     End Sub
  78. End Class
  79.  
  80. <div>
  81.     <asp:DataList id="theDataList" runat="server">
  82.  
  83. <ItemTemplate>
  84.  
  85. <table border="0" cellpadding="0" cellspacing="0" width="500">
  86.  
  87. <tr>
  88.  
  89. <td width="140"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Company Name</strong>:</font></td>
  90.  
  91. <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "CompanyName") %></font></td>
  92.  
  93. </tr>
  94.  
  95. <tr>
  96.  
  97. <td width="110"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Contact Name</strong>:</font></td>
  98.  
  99. <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "ContactName") %></td>
  100.  
  101. </tr>
  102.  
  103. <tr>
  104.  
  105. <td width="110"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Contact Title</strong>:</font></td>
  106.  
  107. <td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "ContactTitle") %></font></td>
  108.  
  109. </tr>
  110.  
  111. </table>
  112.  
  113. </ItemTemplate>
  114.  
  115. <separatortemplate>
  116.  
  117. <hr color="#0099FF" />
  118.  
  119. </separatortemplate>
  120.  
  121. </asp:DataList>
  122.         <asp:LinkButton ID="btnFirst" runat="server" OnClick="btnFirst_Click">First</asp:LinkButton>
  123.         <asp:LinkButton id="btnPrev" Text="Prev" OnClick="btnPrev_Click" runat="server" />
  124.         <asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
  125. <asp:LinkButton id="btnNext" Text="Next" OnClick="btnNext_Click" runat="server" />
  126.         <asp:LinkButton ID="btnLast" runat="server" OnClick="btnLast_Click"/>
  127.         </div>
  128.  
  129.  
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#2: May 11 '09

re: need paging in numbers till last page from db in datalist..??


I'm not exactly sure what your problem is....

But you don't have to do Redirects for paging purposes.

You could simply store the page number that the user is viewing in Session, ViewState, Cookies, or even a HiddenField so that you can set the page index based on the last index that the user was viewing.

In the following example (based on your project), I'm storing the page index that the user is viewing in ViewState.

Here is the ASP code for the page:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="pagingtest.aspx.vb" Inherits="MyNamespace.pagingtest" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6.     <title></title>
  7.       <style type="text/css">
  8.         .prompt
  9.         {
  10.             font-weight: bold;
  11.             display: block;
  12.             width: 150px;
  13.             float: left;
  14.         }
  15.         .value
  16.         {
  17.             font-style: italic;
  18.             float: left;
  19.         }
  20.         .promptValueGroup
  21.         {
  22.             clear: both;
  23.         }
  24.         .listItemStyle
  25.         {
  26.             font-family: Verdana, Arial, Helvetica, sans-serif;
  27.             border-bottom: solid 1px #0099FF;
  28.             margin-bottom:4px;
  29.         }
  30.     </style>
  31. </head>
  32. <body>
  33.     <form id="form1" runat="server">
  34.  
  35.     <asp:DataList ID="theDataList" runat="server">
  36.         <ItemTemplate>
  37.             <div class="listItemStyle">
  38.                 <div class="promptValueGroup">
  39.                     <span class="prompt">Company Name:</span>
  40.                     <span class="value"><%# DataBinder.Eval(Container.DataItem, "CompanyName") %></span>
  41.                 </div>
  42.                 <div class="promptValueGroup">
  43.                     <span class="prompt">Contact Name: </span>
  44.                     <span class="value"><%# DataBinder.Eval(Container.DataItem, "ContactName") %></span>
  45.                 </div>
  46.                 <div class="promptValueGroup">
  47.                     <span class="prompt">Contact Title: </span>
  48.                     <span class="value"><%# DataBinder.Eval(Container.DataItem, "ContactTitle") %></span>
  49.                 </div>
  50.                 <div style="clear:both"></div>
  51.             </div>
  52.         </ItemTemplate>
  53.     </asp:DataList>
  54.     <asp:LinkButton ID="btnFirst" runat="server" OnClick="btnFirst_Click" Text="first"></asp:LinkButton>
  55.     <asp:LinkButton ID="btnPrev" Text="<<" OnClick="btnPrev_Click" runat="server" />
  56.     <asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
  57.     <asp:LinkButton ID="btnNext" Text=">>" OnClick="btnNext_Click" runat="server" />
  58.     <asp:LinkButton ID="btnLast" runat="server" OnClick="btnLast_Click" Text="last"></asp:LinkButton>
  59.  
  60.     </form>
  61. </body>
  62. </html>
  63.  

Here is the VB code that handles the paging for the list (please take note of the comments for more details about what I'm doing)

Expand|Select|Wrap|Line Numbers
  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3.  
  4. Partial Public Class pagingtest
  5.    Inherits System.Web.UI.Page
  6.  
  7.    Private pagedData As New PagedDataSource
  8.    Private currentPage As Integer
  9.  
  10.   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  11.  
  12.      'This method handles the Page Load event.
  13.      'It is executed before an events (like button clicks) 
  14.  
  15.       'Initializing the paged data source and retrieving the 
  16.       'page index of the page that the user is viewing
  17.        initializePagedData()
  18.  
  19.   End Sub
  20.  
  21.   Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
  22.  
  23.  
  24.       'This method handles the aPage's PreRender event .
  25.       'It is executed after your page's event's are finished executing
  26.       '(after the button click code is executed) and just before 
  27.       'everything is converted into HTML.
  28.       'It's pretty much the last step that's done before the page is sent back
  29.       'to the user. 
  30.  
  31.       'At this point, all changes the the paged data source will have been made
  32.       'so I'm calling the method that is responsible for binding the paged data
  33.       'source to the list and saving the paging information for next time.
  34.  
  35.       'Displaying the paged data in the data list
  36.       displayPagedDataInList()
  37.   End Sub
  38.  
  39.   Private Function getTheData() As DataView
  40.  
  41.       'You should replace this code with the code that retrieves
  42.       'The data from your DataBase.
  43.       'Please note that this function has been modified to return a DataView
  44.       'The DataView returned by this method is based on the Table retrieved
  45.       'from the data base....
  46.  
  47.       'Your method would look something like:
  48.  
  49. '        Dim DS As New DataSet()
  50. '        Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
  51.  
  52. '        Dim objSQLAdapter As New SqlDataAdapter("SELECT CompanyName, ContactName, ContactTitle FROM Customers", myConnection)
  53. '        objSQLAdapter.Fill(DS, "Customers")
  54. '        Return DS.Tables(0).DefaultView
  55.  
  56.         Dim dt As New DataTable
  57.         dt.Columns.Add(New DataColumn("CompanyName"))
  58.         dt.Columns.Add(New DataColumn("ContactName"))
  59.         dt.Columns.Add(New DataColumn("ContactTitle"))
  60.  
  61.         Dim dr As DataRow
  62.  
  63.         For i As Integer = 1 To 20
  64.             dr = dt.NewRow
  65.             dr("CompanyName") = "Company " + i.ToString
  66.             dr("ContactName") = "Contact for company  " + i.ToString
  67.             dr("ContactTitle") = "Contact title for company  " + i.ToString
  68.             dt.Rows.Add(dr)
  69.         Next
  70.  
  71.         Return New DataView(dt)
  72.  
  73.   End Function
  74.  
  75.   Private Sub initializePagedData()
  76.         'Retrieving the data source for the paged data source
  77.         Dim ds As DataView = getTheData()
  78.  
  79.         'Setting the data source for the paged data source
  80.         pagedData.DataSource = ds
  81.  
  82.         'Configuring the paged data source to allow paging
  83.         pagedData.AllowPaging = True
  84.  
  85.         'Configuring the paged data source to display 5 items at a time
  86.         pagedData.PageSize = 5
  87.  
  88.          'Retrieving the page index the user was last viewing
  89.         currentPage = CType(ViewState("currPage"), Integer)
  90.   End Sub
  91.  
  92.   Private Sub displayPagedDataInList()
  93.         'Displaying details about which page the user is currently veiwing
  94.         lblCurrentPage.Text = (currentPage + 1).ToString + "/" + pagedData.PageCount.ToString
  95.  
  96.         'Saving the page number
  97.         ViewState("currPage") = currentPage
  98.  
  99.         'Setting current index of the paged data
  100.         pagedData.CurrentPageIndex = currentPage
  101.  
  102.         'Binding the list to the paged data
  103.         theDataList.DataSource = pagedData
  104.         theDataList.DataBind()
  105.  
  106.         'Configuring paging buttons according the page the user's currently viewing
  107.         btnPrev.Enabled = (Not pagedData.IsFirstPage)
  108.         btnFirst.Enabled = (Not pagedData.IsFirstPage)
  109.         btnNext.Enabled = (Not pagedData.IsLastPage)
  110.         btnLast.Enabled = (Not pagedData.IsLastPage)
  111.   End Sub
  112.  
  113.   Protected Sub btnPrev_Click(ByVal sender As Object, ByVal e As EventArgs)
  114.  
  115.         'Moving to the previous page as long as it's not the first page
  116.         If currentPage > 0 Then
  117.             currentPage -= 1
  118.         End If
  119.  
  120.     End Sub
  121.  
  122.   Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs)
  123.  
  124.         'Moving to the next page as long as it's not the last page
  125.         If currentPage < pagedData.PageCount - 1 Then
  126.             currentPage += 1
  127.         End If
  128.  
  129.     End Sub
  130.  
  131.   Protected Sub btnFirst_Click(ByVal sender As Object, ByVal e As EventArgs)
  132.         'Moving the current page to the first page
  133.         currentPage = 0
  134.   End Sub
  135.  
  136.   Protected Sub btnLast_Click(ByVal sender As Object, ByVal e As EventArgs)
  137.         'Moving the current page to the last page 
  138.         currentPage = pagedData.PageCount - 1
  139.   End Sub
  140.  
  141. End Class
  142.  
Newbie
 
Join Date: Jan 2009
Posts: 23
#3: Jul 10 '09

re: need paging in numbers till last page from db in datalist..??


This one worked greatly.....!!
Reply