|
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.ConnectionString.("ConnectionString").ConnectionString
' Initialize connection conn = New SqlConnection(connectionString)
'use a stored proc 'Not getting SQL Statement! adapter = New SqlDataAdapter(SqlDataSource7.SelectCommand, 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.QueryString("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.CurrentExecutionFilePath + "?Page=" + Convert.ToString(intMod + intDiv * DisplayPages + i) + "&where='" + where + "'" End If plcPageNumbers.Controls.Add(lnk)
If intMod + intDiv * DisplayPages + i < LastPage Then Dim lit As New Literal lit.Text = " • " plcPageNumbers.Controls.Add(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.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1) + "&where='" + where + "'" lnkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1&where='" + where + "'" End If
If Not objPds.IsLastPage Then lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1) + "&where='" + where + "'" lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?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/CollectionPager.asp
|
|
|
|