Code Simplified – Viral Sarvaiya

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

Posts Tagged ‘custom paging’

custom paging of the datagrid or gridview

Posted by Viral Sarvaiya on August 20, 2009


hear i demonstrate the custom paging of the datagrid or gridview.

in the client side take the datagrid or grid view

here i take the datagrid,


<table>

<tr>
<td>

<asp:datagrid id="dgMake" runat="server" AutoGenerateColumns="False" Width="100%"
CellPadding="5" GridLines="None" AllowPaging="true">

<Columns>
<asp:TemplateColumn>
<HeaderStyle HorizontalAlign="Left" Width="100"></HeaderStyle>
<HeaderTemplate>Part</HeaderTemplate>
<ItemStyle HorizontalAlign="Left" Width="100"></ItemStyle>
<ItemTemplate><%#Container.DataItem("part")%></ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
<HeaderTemplate>Description</HeaderTemplate>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate><%#Container.DataItem("description")%></ItemTemplate>
</asp:TemplateColumn>

</Columns>
<PagerStyle Visible="False"></PagerStyle>
</asp:datagrid>

</td>
</tr>

in the second raw, i add the link buttons as below,


<tr>
<td valign="top" align="right" bgcolor="#e1e4e6" style="padding:10px 10px 10px 60px;text-align:right;">
<asp:linkbutton id="FirstButton" onclick="PagerButtonClick" runat="server" CssClass="contant" CommandArgument="0"> First Page</asp:linkbutton><asp:label id="lblFirstLine" runat="server">&nbsp;|&nbsp;</asp:label>
<asp:linkbutton id="PrevButton" onclick="PagerButtonClick" runat="server" CssClass="contant" CommandArgument="prev">Prev</asp:linkbutton><asp:label id="lblLine" runat="server">&nbsp;|&nbsp;</asp:label>
<asp:linkbutton id="NextButton" onclick="PagerButtonClick" runat="server" CssClass="contant" CommandArgument="next">Next</asp:linkbutton><asp:label id="lblLastLine" runat="server">&nbsp;|&nbsp;</asp:label>
<asp:linkbutton id="LastButton" onclick="PagerButtonClick" runat="server" CssClass="contant" CommandArgument="last"> Last Page</asp:linkbutton>
</td>
</tr>

in the code-behind file, bind the datagrid as below,

define the page size of the datagrid in the page load


dgMake.PageSize = 50

then call this function in the postback false part of the page load.


sub bind_datagrid()

Dim ds As New DataSet

ds = classobject.fiunctionname()

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

dgMake.DataSource = ds

dgMake.DataBind()

firstprev()

Else

dgMake.DataSource = ds
dgMake.DataBind()

FirstButton.Visible = False
PrevButton.Visible = False
NextButton.Visible = False
LastButton.Visible = False
lblFirstLine.Visible = False
lblLastLine.Visible = False
lblLine.Visible = False

End If
End Sub

Public Sub firstprev()
If dgMake.CurrentPageIndex <> 0 Then
Call Prev_Buttons()
FirstButton.Visible = True
PrevButton.Visible = True
lblFirstLine.Visible = True
Else
FirstButton.Visible = False
PrevButton.Visible = False
lblFirstLine.Visible = False
End If

'To Display Next/Last Page buttons

If dgMake.CurrentPageIndex <> (dgMake.PageCount - 1) Then
Call Next_Buttons()
NextButton.Visible = True
LastButton.Visible = True
lblLastLine.Visible = True
Else
NextButton.Visible = False
LastButton.Visible = False
lblLastLine.Visible = False
End If

If dgMake.CurrentPageIndex > 0 And dgMake.CurrentPageIndex < dgMake.PageCount - 1 Then
lblLine.Visible = True
Else
lblLine.Visible = False
End If

End Sub

Sub Prev_Buttons()
Dim PrevSet As String
If dgMake.CurrentPageIndex + 1 <> 1 Then
PrevSet = dgMake.PageSize
PrevButton.Text = ("Prev")
End If
End Sub

Sub Next_Buttons()
Dim NextSet As String
If dgMake.CurrentPageIndex + 1 < dgMake.PageCount Then
NextSet = dgMake.PageSize
NextButton.Text = ("Next")
End If
End Sub

Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim arg As String = sender.CommandArgument
Select Case arg
Case "next"
'The next Button was Clicked
If (dgMake.CurrentPageIndex < (dgMake.PageCount - 1)) Then
dgMake.CurrentPageIndex += 1
End If

Case "prev"
'The prev button was clicked
If (dgMake.CurrentPageIndex > 0) Then
dgMake.CurrentPageIndex -= 1
End If

Case "last"
'The Last Page button was clicked
dgMake.CurrentPageIndex = (dgMake.PageCount - 1)

Case Else
'The First Page button was clicked
dgMake.CurrentPageIndex = Convert.ToInt32(arg)
End Select
'selpagecombo()
'Now, bind the data!
bind_datagrid()
End Sub

so the page will be bind to 50 records.

enjoy coding…..

Posted in ASP.NET | Tagged: , , | Leave a Comment »

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.

Posted in ASP.NET | Tagged: , , , | Leave a Comment »

 
%d bloggers like this: