<%@ Import NameSpace="System.Data" %>
<%@ Import NameSpace="System.Data.SqlClient" %>
<html>
<head>
<title>Paging in Datalist - by Das</title>

<script language="vb" runat="server">

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
If Not Page.IsPostBack() Then
intPageSize.Text = "5"
intCurrIndex.Text = "0"
DataBind()
End If
End Sub

Private Sub DataBind()
Dim objConn As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim objDA As New SqlDataAdapter("exec sp_das_sales_sel", objConn)
Dim objDS As New DataSet()

If Not Page.IsPostBack() Then
objDA.Fill(objDS)
intRecordCount.Text = CStr(objDS.Tables(0).Rows.Count)
objDS = Nothing
objDS = New DataSet()
End If

objDA.Fill (objDS, Cint(intCurrIndex.Text), CInt(intPageSize.Text), "Sales")

dList.DataSource = objDS.Tables(0).DefaultView
dList.DataBind()
objConn.Close()
PrintStatus()
End Sub

Public Sub ShowFirst(ByVal s As Object, ByVal e As EventArgs)
intCurrIndex.Text = "0"
DataBind()
End Sub


Public Sub ShowPrevious(ByVal s As Object, ByVal e As EventArgs)
intCurrIndex.Text = Cstr(Cint(intCurrIndex.Text) - CInt(intPageSize.Text))
If CInt(intCurrIndex.Text) < 0 Then
intCurrIndex.Text = "0"
End If
DataBind()
End Sub

Public Sub ShowNext(ByVal s As Object, ByVal e As EventArgs)
If CInt(intCurrIndex.Text) + 1 < CInt(intRecordCount.Text) Then
intCurrIndex.Text = CStr(CInt(intCurrIndex.Text) + CInt(intPageSize.Text))
End If
DataBind()
End Sub

Public Sub ShowLast(ByVal s As Object, ByVal e As EventArgs)
Dim tmpInt as Integer

tmpInt = CInt(intRecordCount.Text) Mod CInt(intPageSize.Text)
If tmpInt > 0 Then
intCurrIndex.Text = Cstr(CInt(intRecordCount.Text) - tmpInt)
Else
intCurrIndex.Text = Cstr(CInt(intRecordCount.Text) - CInt(intPageSize.Text))
End If
DataBind()
End Sub

Private Sub PrintStatus()
lblStatus.Text = "Total Records:<b>" & intRecordCount.Text
lblStatus.Text += "</b> - Showing Page:<b> "
lblStatus.Text += CStr(CInt(CInt(intCurrIndex.Text) / CInt(intPageSize.Text)+1))
lblStatus.Text += "</b> of <b>"

If (CInt(intRecordCount.Text) Mod CInt(intPageSize.Text)) > 0 Then
lblStatus.Text += CStr(CInt(CInt(intRecordCount.Text) / CInt(intPageSize.Text)+1))
Else
lblStatus.Text += CStr(CInt(intRecordCount.Text) / CInt(intPageSize.Text))
End If
lblStatus.Text += "</b>"
End Sub

</script>
</head>
<body MS_POSITIONING="GridLayout">

<form id="Form1" method="post" runat="server">
<h2 align="center"><font face="verdana">Paging in DataList</font></h2>
<a name="this"></a>

<asp:DataList ID="dList" Runat="server" Width="100%"
ItemStyle-BackColor="Beige"
ItemStyle-Font-Name="Verdana"
BorderWidth="1"
HeaderStyle-Font-Name="Verdana"
EnableViewState="False">

<HeaderTemplate>
<table width="100%" style="font: 10pt verdana" cellpadding=0 cellspacing=0>
<tr style="background-color:FF0000">
<th align=left><font color="#FFFFFF">Store ID</font></th>
<th align=left><font color="#FFFFFF">Order Number</font></th>
<th align=left><font color="#FFFFFF">Order Date</font></th>
<th align=left><font color="#FFFFFF">Qty</font></th>
<th align=left><font color="#FFFFFF">Description</font></th>
<th align=left><font color="#FFFFFF">Title ID</font></th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr style="background-color:#f5f5dc">
<td><%# DataBinder.Eval(Container.DataItem, "stor_id") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "ord_num") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "ord_date") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "qty") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "payterms") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "title_id") %></td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:DataList>

<table width=100% align="right">
<tr>
<td width=76% align=left>
<asp:label ID="lblStatus"
Runat="server"
Font-Name="verdana"
Font-Size="10pt" />
</td>

<td width=6%>
<a href="datalistpaging.aspx#this"
ID="hrefFirst"
onserverclick="ShowFirst"
runat="server"><b><<</b></a>
</td>

<td width=6%>
<a href="datalistpaging.aspx#this"
ID="hrefPrevious"
onserverclick="ShowPrevious"
runat="server"><b><</b></a>
</td>

<td width=6%>
<a href="datalistpaging.aspx#this"
ID="hrefNext"
onserverclick="ShowNext"
runat="server"><b>></b></a>
</td>

<td width=6%>
<a href="datalistpaging.aspx#this"
ID="hrefLast"
onserverclick="ShowLast"
runat="server"><b>>></b></a>
</td>
</tr>
</table>

<asp:label ID="intCurrIndex"
Visible="False"
Runat="server" />
<asp:label ID="intPageSize"
Visible="False"
Runat="server" />

<asp:label ID="intRecordCount"
Visible="False"
Runat="server" />

</form>
</body>
</html>

Send your comments to das@aspalliance.com        Back to Article list