Microsoft
Software
Hardware
Network
Question : ASP.NET 2.0 Datalist and Paging
I am trying to set up paging with a .NET 2.0 Datalist. I have pretty much succeeded to implement it using a PagedDataSource. My only remaining issue is that I want the page controls to look something like:
<| << 1 2 3 4 5 >> |>
I've got it working except that the current page is ALWAYS the first page in the list. So, if I click on 5, 5 becomes the first item in the list. With the exception of Page 1 & 2, I'd like the first page in the list to be the third item. How do I do that?
Here's what I've got:
Dim conn As SqlConnection
Dim ds As New DataSet
Dim adapter As SqlDataAdapter
' Read the connection string from Web.config
Dim connectionString As String = ConfigurationManager.Conne
ctionStrin
g.("Connec
tionString
").Connect
ionString
' Initialize connection
conn = New SqlConnection(connectionSt
ring)
'use a stored proc
'Not getting SQL Statement!
adapter = New SqlDataAdapter(SqlDataSour
ce7.Select
Command, conn)
adapter.Fill(ds)
Dim objPds As PagedDataSource = New PagedDataSource
objPds.DataSource = ds.Tables(0).DefaultView
objPds.AllowPaging = True
objPds.PageSize = 10
Dim CurPage As Integer
Dim LastPage As Integer
Dim DisplayPages As Integer
If Not (Request.QueryString("Page
") Is Nothing) Then
CurPage = Convert.ToInt32(Request.Qu
eryString(
"Page"))
Else
CurPage = 1
End If
objPds.CurrentPageIndex = CurPage - 1
LastPage = objPds.PageCount
DisplayPages = 5
lblCurrentPage.Text = "Page " + CurPage.ToString + " of " + LastPage.ToString()
Dim i As Integer
Dim intMod As Integer
Dim intDiv As Integer
For i = 0 To DisplayPages - 1
Dim lnk As New HyperLink
intDiv = Fix(CurPage \ DisplayPages)
intMod = CurPage Mod DisplayPages
If intMod + intDiv * DisplayPages + i <= LastPage Then
lnk.ID = "lnk" + (intMod + intDiv * DisplayPages + i).ToString()
lnk.Text = (intMod + intDiv * DisplayPages + i).ToString()
lnk.CssClass = "PropertyPageLink"
lnk.ToolTip = (intMod + intDiv * DisplayPages + i).ToString() + " of " + LastPage.ToString()
If intMod + intDiv * DisplayPages + i <> CurPage Then
lnk.NavigateUrl = Request.CurrentExecutionFi
lePath + "?Page=" + Convert.ToString(intMod + intDiv * DisplayPages + i) + "&where='" + where + "'"
End If
plcPageNumbers.Controls.Ad
d(lnk)
If intMod + intDiv * DisplayPages + i < LastPage Then
Dim lit As New Literal
lit.Text = " • "
plcPageNumbers.Controls.Ad
d(lit)
End If
End If
Next
If intMod + intDiv * DisplayPages + i <= LastPage Then
lblPostPageNumbers.Visible
= True
Else
lblPostPageNumbers.Visible
= False
End If
If CurPage > DisplayPages Then
lblPrePageNumbers.Visible = True
End If
If Not objPds.IsFirstPage Then
lnkPrev.NavigateUrl = Request.CurrentExecutionFi
lePath + "?Page=" + Convert.ToString(CurPage - 1) + "&where='" + where + "'"
lnkFirst.NavigateUrl = Request.CurrentExecutionFi
lePath + "?Page=1&where='" + where + "'"
End If
If Not objPds.IsLastPage Then
lnkNext.NavigateUrl = Request.CurrentExecutionFi
lePath + "?Page=" + Convert.ToString(CurPage + 1) + "&where='" + where + "'"
lnkLast.NavigateUrl = Request.CurrentExecutionFi
lePath + "?Page=" + objPds.PageCount.ToString(
) + "&where='" + where + "'"
End If
ResultsList.DataSource = objPds
ResultsList.DataBind()
conn.Close()
Thanks!
Jennifer
Answer : ASP.NET 2.0 Datalist and Paging
You may have a look at the collectionPager, it will make paging a lot of easier instead of doing hard work by your own.
http://www.codeproject.com
/aspnet/Co
llectionPa
ger.asp
Random Solutions
Command button code
Can't save Access 2003 report with specific printer under Access 2007
how to create a TreeView and Bind its nodes to a DataTable?
How can i hide root level of TreeView in Silverlight?
MS Access 2003 ... Add customized code to form created by Wizard
Restrict Formatting & Editing, Start Enforcement Option not Available
How to change open directory in msoFileDialogOpen (Excel Vba)
Windows SBS 2008 DCPROMO fails - The Replication Operation Failed to Allocate Memory
query to show anything due in the current week
Solve this backup error, "The storage control block address is invalid."