473,385 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

23
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.  
May 7 '09 #1
2 2782
Frinavale
9,735 Expert Mod 8TB
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.  
May 11 '09 #2
sureshl
23
This one worked greatly.....!!
Jul 10 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: enak | last post by:
I can not get my datagrid to page. I have a datagrid that I can sort 2 of the columns. This works great. I added paging and when I display the dg it shows 5 pages. (I am showing page numbers at...
2
by: David | last post by:
Hello. How can I enable and manage paging capability on DataList control like DataGrid? Thank you.
1
by: SouthSpawn | last post by:
Hello, I have an asp.net application that will use a SQL Server DBMS for the backend. On my asp.net form. I have a next, previous, last and first buttons on my form. Basically, this...
1
by: Maria | last post by:
Hi, I have read about paging, segmentation and paged segmentation and I believe I have (nearly) understood how these techniques are implemented in hardware. However, I am till confused about the...
2
by: farhad13841384 | last post by:
Hi , I Hope You fine. I have some problem with this code for paging in asp.net this bottom code work correctly without any error but when I try to place separate code in .VB file then error is...
0
by: needin4mation | last post by:
Hi, I have read many different datalist paging solutions, including using the ROW_NUMBER() with Sql Server 2005. I have googled various methods to use with lower versions of Sql Server. I...
1
by: Aussie Rules | last post by:
Hi, Is it possible to have some sort of paging (prev/next page) feature with the data list. I have a datalist, that can display several hundred items, and I want to have say 20 to a page. ...
1
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, I have an application web asp.net 2.0. I have a aspx page with Datalist and paging. Now, I want that the user can select several rows of DataList (multiple selection) and maintain...
2
by: wallconor | last post by:
Hi, I am having a problem using Dreamweaver CS3 standard recordset paging behavior. It doesn’t seem to work when I pass parameter values from a FORM on my search page, to the recordset on my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.