Online Resources for Web Developers and Professionals - BETA

  search

Using Pagination with DataList Control in ASP.NET 2.0

In this article i will show you how you can manage advance level pagination with DataList, Repeater , or other controls with ASP.NET 2.0.

I create a small program below which provide you clear understanding how you can implement pagination with Datalist in ASP.NET 2.0. Although this program is not completed and full and functional but will provide you clear understanding about Pagination.

Example :

Product.aspx

<asp:DataList ID="product_list" AlternatingItemStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center"
AlternatingItemStyle-VerticalAlign="top" ItemStyle-VerticalAlign="top" runat="server"
CellPadding="0" CellSpacing="0" Width="100%" RepeatColumns="4" RepeatDirection="horizontal"
EnableViewState="true">
<ItemTemplate>

<!--- List functionality -->

</ItemTemplate>
</DataList>

<!-- here i implement pagination, i use Repeater control for pagination numbers. -->

<!-- Prev Button -->
<asp:LinkButton ID="lnk_Prev" runat="server" Text="Prev"></asp:LinkButton>
<!-- Repeater control for pagination -->
<asp:Repeater ID="rptPages" runat="server">
<ItemTemplate>
<asp:LinkButton ID="btnPage" CommandName="Page" CommandArgument="<%# Container.DataItem %>"
Text='<%# Container.DataItem %>' runat="server"></asp:LinkButton>&nbsp;
</ItemTemplate>
</asp:Repeater>

<!-- Next Button -->
<asp:LinkButton ID="lnk_Next" runat="server" Text="Next"></asp:LinkButton>

You can customize the code with you own requirement.

Code Behind:

'// Set Property for Pagination, This property will store Page Number
Public Property PageNumber() As Integer
Get
If Not ViewState("PageNumber") Is Nothing Then
Return CInt(ViewState("PageNumber"))
Else
Return 1
End If
End Get
Set(ByVal value As Integer)
ViewState("PageNumber") = value
End Set
End Property

<!-- This code will set runtime event for Repeater control -->
Protected Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
AddHandler rptPages.ItemCommand, AddressOf Me.rptPages_ItemCommand
End Sub

Protected Sub rptPages_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs)
Dim btnPage As LinkButton = CType(e.Item.FindControl("btnPage"), LinkButton)
PageNumber = CInt(btnPage.Text)
Load_Products()
End Sub

Protected Sub rptPages_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPages.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim btnPage As LinkButton = CType(e.Item.FindControl("btnPage"), LinkButton)
If btnPage.Text = PageNumber Then
btnPage.CssClass = "pagination_link_selected"
Else
btnPage.CssClass = "pagination_link"
End If
End If
End Sub

Private Sub Load_Products()

<!-- Load Products that will be use to load products. -->
Dim ds as dataset = some method
if ds.tables(0).rows.count<>0
Process_Pagination(ds)
Else
ShowMessage("No record found")
End if
End Sub

<!--Process Pagination contains the real logic for Pagination. -->

Protected Sub Process_Pagination(ByVal ds As DataSet)


Dim objPds As New PagedDataSource()
objPds.DataSource = ds.Tables(0).DefaultView
objPds.AllowPaging = True
Dim pagesize As Integer = _paginationdalc.GetPageSize("BrowseMember", "MemberList")
objPds.PageSize = pagesize
objPds.CurrentPageIndex = (PageNumber - 1)

If Not objPds.IsFirstPage Then
lnk_Prev.Visible = True
lnk_prev_top.Visible = True
Else
lnk_Prev.Visible = False
lnk_prev_top.Visible = False
End If

If Not objPds.IsLastPage Then
lnk_Next.Visible = True
lnk_next_top.Visible = True
Else
lnk_Next.Visible = False
lnk_next_top.Visible = False
End If

<!-- Create and return links -->
Dim arr As ArrayList =Return_Pagination_Link(objPds.PageCount, 15, PageNumber)
If objPds.PageCount > 1 Then
rptPages.Visible = True
rptPages.DataSource = arr
rptPages.DataBind()

rptPages_top.Visible = True
rptPages_top.DataSource = arr
rptPages_top.DataBind()
Else
rptPages.Visible = False
rptPages_top.Visible = False
End If

product_list.DataSource = objPds
product_list.DataBind()
End Sub

<!-- This will create dynamic links based on total pages. -->
Public Function Return_Pagination_Link(ByVal TotalPages As Integer, ByVal Total_Links As Integer, ByVal SelectedPage As Integer) As ArrayList
Dim i As Integer
Dim arr As New ArrayList
If TotalPages < Total_Links Then
For i = 1 To TotalPages
arr.Add(i)
Next
Else
Dim startindex As Integer = SelectedPage
Dim lowerbound As Integer = startindex - CInt(Math.Floor(Total_Links / 2))
Dim upperbound As Integer = startindex + CInt(Math.Floor(Total_Links / 2))
If lowerbound < 1 Then
'calculate the difference and increment the upper bound
upperbound = upperbound + (1 - lowerbound)
lowerbound = 1
End If
'if upperbound is greater than total page is
If upperbound > TotalPages Then
'calculate the difference and decrement the lower bound
lowerbound = lowerbound - (upperbound - TotalPages)
upperbound = TotalPages
End If
For i = lowerbound To upperbound
arr.Add(i)
Next
End If
Return arr


End Function

 

That's it , above is complete code for handling pagination with datalist control.