Hi carrzkiss,
Please refer below sample.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
C#
<asp:DataList ID="dlCustomers" runat="server" RepeatDirection="Horizontal" RepeatColumns="5">
<ItemTemplate>
<table cellpadding="2" cellspacing="0" class="Item">
<tr>
<td class="body">
<%# Eval("ContactName") %><br />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<asp:Repeater ID="rptPager" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>'
CssClass='<%# Convert.ToBoolean(Eval("Enabled"))? "page_enabled": "page_disabled"%>'
OnClick="Page_Changed" OnClientClick='<%# !Convert.ToBoolean(Eval("Enabled"))? "return false;": "" %>'></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
<asp:Label runat="server" ID="CurrentPageNumber"></asp:Label>
VB.Net
<asp:DataList ID="dlCustomers" runat="server" RepeatDirection="Horizontal" RepeatColumns="5">
<ItemTemplate>
<table cellpadding="2" cellspacing="0" class="Item">
<tr>
<td class="body">
<%# Eval("ContactName") %><br />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<asp:Repeater ID="rptPager" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>'
CssClass='<%# If(Convert.ToBoolean(Eval("Enabled")), "page_enabled", "page_disabled")%>'
OnClick="Page_Changed" OnClientClick='<%# If(Not Convert.ToBoolean(Eval("Enabled")), "return false;", "") %>'></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
<asp:Label runat="server" ID="CurrentPageNumber"></asp:Label>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Collections.Generic
Code
C#
private int PageSizes = 30;
private int PageIndex
{
get
{
if (!string.IsNullOrEmpty(Request.QueryString["pageIndex"]))
{
return Convert.ToInt32(Request.QueryString["pageIndex"]);
}
else
{
return 0;
}
}
}
private int PageSize
{
get
{
if (!string.IsNullOrEmpty(Request.QueryString["pageSize"]))
{
return Convert.ToInt32(Request.QueryString["pageSize"]);
}
else
{
return 4;
}
}
}
private int PageCount
{
get
{
if (TotalRowCount <= 0 || PageSize <= 0)
{
return 1;
}
else
{
return (int)Math.Round((TotalRowCount + PageSize - 1) / (double)PageSize);
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetCustomersPageWise(1);
}
}
private void GetCustomersPageWise(int pageIndex)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (var con = new SqlConnection(constr))
{
using (var cmd = new SqlCommand("GetCustomersPageWise", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSizes);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
con.Open();
IDataReader idr = cmd.ExecuteReader();
dlCustomers.DataSource = idr;
dlCustomers.DataBind();
idr.Close();
con.Close();
int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
PopulatePager(recordCount, pageIndex);
}
}
}
private void PopulatePager(int recordCount, int currentPage)
{
//double dblPageCount = System.Convert.ToDouble(System.Convert.ToDecimal(recordCount) / (double)System.Convert.ToDecimal(PageSizes));
double dblPageCount = (double)(recordCount / (decimal)PageSizes);
int pageCount = System.Convert.ToInt32(Math.Ceiling(dblPageCount));
List<ListItem> pages = new List<ListItem>();
if (pageCount > 0)
{
if (currentPage > 1)
pages.Add(new ListItem("First", "1"));
if (currentPage > 1)
pages.Add(new ListItem("Previous", (currentPage - 1).ToString()));
for (int i = 1; i <= pageCount; i++)
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
if (currentPage < pageCount)
pages.Add(new ListItem("Next", (currentPage + 1).ToString()));
if (currentPage != pageCount)
pages.Add(new ListItem("Last", pageCount.ToString()));
}
rptPager.DataSource = pages;
rptPager.DataBind();
CurrentPageNumber.Text = String.Format("Showing page {0} of {1}: Total of Records: {2}", currentPage, pageCount, recordCount);
}
protected void Page_Changed(object sender, EventArgs e)
{
int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
GetCustomersPageWise(pageIndex);
}
private int TotalRowCount
{
get
{
object o = ViewState["TotalRowCount"];
if ((o == null))
return -1;
else
return Convert.ToInt32(o);
}
set
{
ViewState["TotalRowCount"] = value;
}
}
VB.Net
Private PageSizes As Integer = 30
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
GetCustomersPageWise(1)
End If
End Sub
Private ReadOnly Property PageIndex As Integer
Get
If Not String.IsNullOrEmpty(Request.QueryString("pageIndex")) Then
Return Convert.ToInt32(Request.QueryString("pageIndex"))
Else
Return 0
End If
End Get
End Property
Private ReadOnly Property PageSize As Integer
Get
If Not String.IsNullOrEmpty(Request.QueryString("pageSize")) Then
Return Convert.ToInt32(Request.QueryString("pageSize"))
Else
Return 4
End If
End Get
End Property
Private ReadOnly Property PageCount As Integer
Get
If PageCount <= 0 OrElse PageSize <= 0 Then
Return 1
Else
Return CInt(Math.Round((PageCount + PageSize - 1) / CDbl(PageSize)))
End If
End Get
End Property
Private Sub GetCustomersPageWise(pageIndex As Integer)
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(conString)
Using cmd As New SqlCommand("GetCustomersPageWise", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@PageIndex", pageIndex)
cmd.Parameters.AddWithValue("@PageSize", PageSizes)
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4)
cmd.Parameters("@RecordCount").Direction = ParameterDirection.Output
con.Open()
Dim idr As IDataReader = cmd.ExecuteReader()
dlCustomers.DataSource = idr
dlCustomers.DataBind()
idr.Close()
con.Close()
Dim recordCount As Integer = Convert.ToInt32(cmd.Parameters("@RecordCount").Value)
Me.PopulatePager(recordCount, pageIndex)
End Using
End Using
End Sub
Private Sub PopulatePager(recordCount As Integer, currentPage As Integer)
Dim dblPageCount As Double = CDbl(CDec(recordCount) / CDec(PageSizes))
Dim pageCount As Integer = CInt(Math.Ceiling(dblPageCount))
Dim pages As New List(Of ListItem)()
If pageCount > 0 Then
If currentPage > 1 Then
pages.Add(New ListItem("First", "1"))
End If
If currentPage > 1 Then
pages.Add(New ListItem("Previous", (currentPage - 1).ToString()))
End If
For i As Integer = 1 To pageCount
pages.Add(New ListItem(i.ToString(), i.ToString(), i <> currentPage))
Next
If currentPage < pageCount Then
pages.Add(New ListItem("Next", (currentPage + 1).ToString()))
End If
If currentPage <> pageCount Then
pages.Add(New ListItem("Last", pageCount.ToString()))
End If
End If
rptPager.DataSource = pages
rptPager.DataBind()
CurrentPageNumber.Text = String.Format("Showing page {0} of {1}: Total of Records: {2}", currentPage, pageCount, recordCount)
End Sub
Protected Sub Page_Changed(sender As Object, e As EventArgs)
Dim pageIndex As Integer = Integer.Parse(TryCast(sender, LinkButton).CommandArgument)
Me.GetCustomersPageWise(pageIndex)
End Sub
Private Property TotalRowCount As Integer
Get
Dim o As Object = ViewState("TotalRowCount")
If (o Is Nothing) Then
Return -1
Else
Return Convert.ToInt32(o)
End If
End Get
Set(ByVal value As Integer)
ViewState("TotalRowCount") = value
End Set
End Property
Screenshot