need paging in numbers till last page from db in datalist..?? | Newbie | | Join Date: Jan 2009
Posts: 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...? -
Imports System.Data
-
Imports System.Data.SqlClient
-
-
Partial Public Class pagingtest
-
Inherits System.Web.UI.Page
-
Private pagedData As New PagedDataSource()
-
Private CurPage As Integer = 1
-
-
Private Sub Page_Load(ByVal obj As [Object], ByVal e As EventArgs) Handles Me.Load
-
doPaging()
-
End Sub
-
-
Public Function getTheData() As DataTable
-
-
Dim DS As New DataSet()
-
Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
-
-
Dim objSQLAdapter As New SqlDataAdapter("SELECT CompanyName, ContactName, ContactTitle FROM Customers", myConnection)
-
objSQLAdapter.Fill(DS, "Customers")
-
Return (DS.Tables(0))
-
-
-
End Function
-
-
Private Sub doPaging()
-
-
pagedData.DataSource = getTheData().DefaultView
-
-
pagedData.AllowPaging = True
-
pagedData.PageSize = 4
-
Try
-
If Request("Page").ToString() IsNot Nothing Then
-
CurPage = Int32.Parse(Request("Page").ToString())
-
Else
-
CurPage = 1
-
End If
-
-
pagedData.CurrentPageIndex = CurPage - 1
-
Catch ex As Exception
-
-
pagedData.CurrentPageIndex = 0
-
End Try
-
-
btnPrev.Enabled = (Not pagedData.IsFirstPage)
-
'btnPrev.Visible = (!pagedData.IsFirstPage);
-
-
btnFirst.Enabled = (Not pagedData.IsFirstPage)
-
-
btnNext.Enabled = (Not pagedData.IsLastPage)
-
'btnNext.Visible = (!pagedData.IsLastPage);
-
-
btnLast.Enabled = (Not pagedData.IsLastPage)
-
-
'pagedData.CurrentPageIndex = CurPage - 1;
-
-
lblCurrentPage.Text = ("Page: " & CurPage.ToString() & " of ") & pagedData.PageCount.ToString()
-
-
theDataList.DataSource = pagedData
-
-
theDataList.DataBind()
-
End Sub
-
-
Protected Sub btnPrev_Click(ByVal sender As Object, ByVal e As EventArgs)
-
Response.Redirect((Request.CurrentExecutionFilePath & "?Page=") & (CurPage - 1))
-
End Sub
-
-
Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs)
-
Response.Redirect((Request.CurrentExecutionFilePath & "?Page=") & (CurPage + 1))
-
End Sub
-
-
Protected Sub btnFirst_Click(ByVal sender As Object, ByVal e As EventArgs)
-
Response.Redirect((Request.CurrentExecutionFilePath & "?Page=") & (1))
-
End Sub
-
-
Protected Sub btnLast_Click(ByVal sender As Object, ByVal e As EventArgs)
-
Response.Redirect((Request.CurrentExecutionFilePath & "?Page=") & (pagedData.PageCount))
-
End Sub
-
End Class
-
-
<div>
-
<asp:DataList id="theDataList" runat="server">
-
-
<ItemTemplate>
-
-
<table border="0" cellpadding="0" cellspacing="0" width="500">
-
-
<tr>
-
-
<td width="140"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Company Name</strong>:</font></td>
-
-
<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "CompanyName") %></font></td>
-
-
</tr>
-
-
<tr>
-
-
<td width="110"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Contact Name</strong>:</font></td>
-
-
<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "ContactName") %></td>
-
-
</tr>
-
-
<tr>
-
-
<td width="110"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Contact Title</strong>:</font></td>
-
-
<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif"><%# DataBinder.Eval(Container.DataItem, "ContactTitle") %></font></td>
-
-
</tr>
-
-
</table>
-
-
</ItemTemplate>
-
-
<separatortemplate>
-
-
<hr color="#0099FF" />
-
-
</separatortemplate>
-
-
</asp:DataList>
-
<asp:LinkButton ID="btnFirst" runat="server" OnClick="btnFirst_Click">First</asp:LinkButton>
-
<asp:LinkButton id="btnPrev" Text="Prev" OnClick="btnPrev_Click" runat="server" />
-
<asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
-
<asp:LinkButton id="btnNext" Text="Next" OnClick="btnNext_Click" runat="server" />
-
<asp:LinkButton ID="btnLast" runat="server" OnClick="btnLast_Click"/>
-
</div>
-
-
|  | Site Moderator | | Join Date: Oct 2006 Location: The Great White North
Posts: 5,066
| | | 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: -
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="pagingtest.aspx.vb" Inherits="MyNamespace.pagingtest" %>
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head runat="server">
-
<title></title>
-
<style type="text/css">
-
.prompt
-
{
-
font-weight: bold;
-
display: block;
-
width: 150px;
-
float: left;
-
}
-
.value
-
{
-
font-style: italic;
-
float: left;
-
}
-
.promptValueGroup
-
{
-
clear: both;
-
}
-
.listItemStyle
-
{
-
font-family: Verdana, Arial, Helvetica, sans-serif;
-
border-bottom: solid 1px #0099FF;
-
margin-bottom:4px;
-
}
-
</style>
-
</head>
-
<body>
-
<form id="form1" runat="server">
-
-
<asp:DataList ID="theDataList" runat="server">
-
<ItemTemplate>
-
<div class="listItemStyle">
-
<div class="promptValueGroup">
-
<span class="prompt">Company Name:</span>
-
<span class="value"><%# DataBinder.Eval(Container.DataItem, "CompanyName") %></span>
-
</div>
-
<div class="promptValueGroup">
-
<span class="prompt">Contact Name: </span>
-
<span class="value"><%# DataBinder.Eval(Container.DataItem, "ContactName") %></span>
-
</div>
-
<div class="promptValueGroup">
-
<span class="prompt">Contact Title: </span>
-
<span class="value"><%# DataBinder.Eval(Container.DataItem, "ContactTitle") %></span>
-
</div>
-
<div style="clear:both"></div>
-
</div>
-
</ItemTemplate>
-
</asp:DataList>
-
<asp:LinkButton ID="btnFirst" runat="server" OnClick="btnFirst_Click" Text="first"></asp:LinkButton>
-
<asp:LinkButton ID="btnPrev" Text="<<" OnClick="btnPrev_Click" runat="server" />
-
<asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
-
<asp:LinkButton ID="btnNext" Text=">>" OnClick="btnNext_Click" runat="server" />
-
<asp:LinkButton ID="btnLast" runat="server" OnClick="btnLast_Click" Text="last"></asp:LinkButton>
-
-
</form>
-
</body>
-
</html>
-
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) -
Imports System.Data
-
Imports System.Data.SqlClient
-
-
Partial Public Class pagingtest
-
Inherits System.Web.UI.Page
-
-
Private pagedData As New PagedDataSource
-
Private currentPage As Integer
-
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
-
'This method handles the Page Load event.
-
'It is executed before an events (like button clicks)
-
-
'Initializing the paged data source and retrieving the
-
'page index of the page that the user is viewing
-
initializePagedData()
-
-
End Sub
-
-
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
-
-
-
'This method handles the aPage's PreRender event .
-
'It is executed after your page's event's are finished executing
-
'(after the button click code is executed) and just before
-
'everything is converted into HTML.
-
'It's pretty much the last step that's done before the page is sent back
-
'to the user.
-
-
'At this point, all changes the the paged data source will have been made
-
'so I'm calling the method that is responsible for binding the paged data
-
'source to the list and saving the paging information for next time.
-
-
'Displaying the paged data in the data list
-
displayPagedDataInList()
-
End Sub
-
-
Private Function getTheData() As DataView
-
-
'You should replace this code with the code that retrieves
-
'The data from your DataBase.
-
'Please note that this function has been modified to return a DataView
-
'The DataView returned by this method is based on the Table retrieved
-
'from the data base....
-
-
'Your method would look something like:
-
-
' Dim DS As New DataSet()
-
' Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
-
-
' Dim objSQLAdapter As New SqlDataAdapter("SELECT CompanyName, ContactName, ContactTitle FROM Customers", myConnection)
-
' objSQLAdapter.Fill(DS, "Customers")
-
' Return DS.Tables(0).DefaultView
-
-
Dim dt As New DataTable
-
dt.Columns.Add(New DataColumn("CompanyName"))
-
dt.Columns.Add(New DataColumn("ContactName"))
-
dt.Columns.Add(New DataColumn("ContactTitle"))
-
-
Dim dr As DataRow
-
-
For i As Integer = 1 To 20
-
dr = dt.NewRow
-
dr("CompanyName") = "Company " + i.ToString
-
dr("ContactName") = "Contact for company " + i.ToString
-
dr("ContactTitle") = "Contact title for company " + i.ToString
-
dt.Rows.Add(dr)
-
Next
-
-
Return New DataView(dt)
-
-
End Function
-
-
Private Sub initializePagedData()
-
'Retrieving the data source for the paged data source
-
Dim ds As DataView = getTheData()
-
-
'Setting the data source for the paged data source
-
pagedData.DataSource = ds
-
-
'Configuring the paged data source to allow paging
-
pagedData.AllowPaging = True
-
-
'Configuring the paged data source to display 5 items at a time
-
pagedData.PageSize = 5
-
-
'Retrieving the page index the user was last viewing
-
currentPage = CType(ViewState("currPage"), Integer)
-
End Sub
-
-
Private Sub displayPagedDataInList()
-
'Displaying details about which page the user is currently veiwing
-
lblCurrentPage.Text = (currentPage + 1).ToString + "/" + pagedData.PageCount.ToString
-
-
'Saving the page number
-
ViewState("currPage") = currentPage
-
-
'Setting current index of the paged data
-
pagedData.CurrentPageIndex = currentPage
-
-
'Binding the list to the paged data
-
theDataList.DataSource = pagedData
-
theDataList.DataBind()
-
-
'Configuring paging buttons according the page the user's currently viewing
-
btnPrev.Enabled = (Not pagedData.IsFirstPage)
-
btnFirst.Enabled = (Not pagedData.IsFirstPage)
-
btnNext.Enabled = (Not pagedData.IsLastPage)
-
btnLast.Enabled = (Not pagedData.IsLastPage)
-
End Sub
-
-
Protected Sub btnPrev_Click(ByVal sender As Object, ByVal e As EventArgs)
-
-
'Moving to the previous page as long as it's not the first page
-
If currentPage > 0 Then
-
currentPage -= 1
-
End If
-
-
End Sub
-
-
Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs)
-
-
'Moving to the next page as long as it's not the last page
-
If currentPage < pagedData.PageCount - 1 Then
-
currentPage += 1
-
End If
-
-
End Sub
-
-
Protected Sub btnFirst_Click(ByVal sender As Object, ByVal e As EventArgs)
-
'Moving the current page to the first page
-
currentPage = 0
-
End Sub
-
-
Protected Sub btnLast_Click(ByVal sender As Object, ByVal e As EventArgs)
-
'Moving the current page to the last page
-
currentPage = pagedData.PageCount - 1
-
End Sub
-
-
End Class
-
| | Newbie | | Join Date: Jan 2009
Posts: 23
| | | re: need paging in numbers till last page from db in datalist..??
This one worked greatly.....!!
|  | Similar Visual Basic .NET bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|