Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Datalist Custom Paging

Posted by Viral Sarvaiya on March 13, 2009

In this article i will explain a method for providing custom paging for datalist or repeater.

As you know the datalist is a very powerful control with one drawback that it does not have built-in paging capability, a feature the DataGrid offers. to provide paging to datalist or repeater we can either use “PagedDataSource” class, found in the System.Web.UI.WebControls namespace for auto paging like the datagrid or implement custom paging functionality.

But I describe new things for datalist paging, i use plasecontrol for paging as below.

Step 1 :  Make a Default.aspx page

<head>

</head>

<body>

<asp:PlaceHolder ID=”phtopsort” runat=”server”></asp:PlaceHolder>

<asp:DataList ID=”dlFabricsort” runat=”server” DataKeyField=”Fabricid” ShowFooter=”false” EnableViewState=”false”
ShowHeader=”false” RepeatColumns=”3″ RepeatDirection=”Horizontal” HorizontalAlign=”left”
CellPadding=”0″ CellSpacing=”0″ Width=”93%” BackColor=”#F4F1E6″>

<ItemStyle VerticalAlign=”Top” Height=”100%” HorizontalAlign=”Left”/>
<ItemTemplate>
<table width=”100%”>

<asp:Label ID=”Label1″ runat=”server” Text ='<%# container.dataitem(“fabricname”)%>’></asp:Label>

</table>
</ItemTemplate>
</asp:DataList>

</body>

Step 2 : In Default.aspx.vb

Dim total As Integer

Dim totalpages As Integer


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = False Then

If Request.QueryString(“p”) = Nothing Then
page1 = 1
ElseIf Request.QueryString(“p”) <= 0 Then
page1 = 1
ElseIf Request.QueryString(“p”) = “” Then
page1 = 1
ElseIf Request.QueryString(“p”) <> “” Then
page1 = Request.QueryString(“p”)
End If

datalist_sortfabric()

End If
End Sub

sub datalist_sortfabric()

Dim dssort As New DataSet
Dim dssort1 As New DataSet

dssort = objfabric.SelectSortFabric()  ‘ Select full Dataset

If dssort.Tables(0).Rows.Count > 0 Then

total = dssort.Tables(0).Rows.Count
size = 15   ‘total recordper page

Dim tp As Double = total / size
Dim arr As Array = tp.ToString.Split(“.”)
totalpages = arr(0)
If total Mod size <> 0 Then
totalpages = totalpages + 1
End If

dssort1 = objfabric.SelectSortFabricPaging((page1 – 1) * size, size)

record.Value = dssort.Tables(0).Rows.Count

dlFabricsort.DataSource = dssort1
dlFabricsort.DataBind()

dynamic_paging()

end if

end sub

sub dynamic_paging()

Dim str As String = “”
phtopsort.Controls.Clear()
Dim i As Integer

‘**********First

If page1 = 1 Then
str += “”
Else
str += “<a href=’Default.aspx?p=1′ class=’link12′>First</a>&nbsp;
End If

‘**********pre

If page1 = 1 Then
str += “”
Else
str += “<a href=’Default.aspx?p=” & page1 – 1 & “‘ class=’link12’>Previous</a> &nbsp; ”
End If

‘**********middle
For i = 0 To totalpages – 1

Dim tmp As String = “”
If i = 0 Then
If i + 1 = page1 Then
tmp = “<a href=’Default.aspx?p=” & i + 1 & “‘ class=’link12′>” & i + 1 & “</a>”
Else
tmp = “<a href=’Default.aspx?p=” & i + 1 & “‘ class=’link12′>” & i + 1 & “</a>”
End If
Else
If i + 1 = page1 Then
tmp = ” | <strong> <a href=’Default.aspx?p=” & i + 1 & “‘ class=’link12′>” & i + 1 & “</a></strong>”
Else
tmp = ” | <a href=’Default.aspx?p=” & i + 1 & “‘ class=’link12’>” & i + 1 & “</a>”
End If

End If

If i >= page1 – 6 And i <= page1 + 4 Then
str += tmp
Else
str += “”
End If
Next
‘**********Next

If page1 = totalpages Then
str += “”
Else
str += “&nbsp;&nbsp;&nbsp;<a href=’Default.aspx?p=” & page1 + 1 & “‘ class=’link12’>Next</a>”
End If

‘**********Last

If page1 = totalpages Then
str += “”
Else
str += “&nbsp;&nbsp;&nbsp;<a href=’Default.aspx?p=” & totalpages & “‘ class=’link12’>Last</a>”
End If

‘**********
Dim tab As Table = New Table
tab.Controls.Clear()
tab.CellSpacing = 0
tab.CellPadding = 0
tab.BorderWidth = Unit.Point(0)

Dim tabrow As TableRow = New TableRow
tab.Controls.Add(tabrow)

Dim tabcell As TableCell = New TableCell
tabcell.VerticalAlign = VerticalAlign.Middle
tabcell.HorizontalAlign = HorizontalAlign.Center

tabcell.Text = str
tabrow.Controls.Add(tabcell)
phtopsort.Controls.Add(tab)

end sub

here SelectSortFabricPaging() function contains  page index and page size means when your dataAdapter fills value

objAdp.Fill(ds, pindex, psize, “fabrics”)

here  ds is dataset, pindex is current page index, psize is size of page, and “fabreics” is name of table from you get the records.

And last more one things to that when we build a string for placeholder, we write a page name (default.aspx) if your page contains some querystring then write it there.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: