Hi George616,
You need to use custom paging and set lebel for displaying the text.
For more details refer below article.
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
SQL
--EXEC Customers_GetCustomersPaging 2,10,NULL
CREATE PROCEDURE Customers_GetCustomersPaging
@PageIndex INT
,@PageSize INT
,@RecordCount INT OUT
AS
BEGIN
SELECT ROW_NUMBER() OVER(ORDER BY CustomerId) RowNumber
,CustomerId
,ContactName
,CompanyName
,ContactTitle
INTO #Temp
FROM Customers
SELECT @RecordCount = COUNT(*) FROM #Temp
SELECT * FROM #Temp
WHERE (RowNumber BETWEEN ((@PageIndex-1) * @PageSize) + 1 AND (@PageIndex * @PageSize)) OR @PageIndex = - 1
DROP TABLE #Temp
END
HTML
C#
<div style="width: 450px">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="Item" Width="450px">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="Id" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="CompanyName" HeaderText="Company" />
</Columns>
</asp:GridView>
<br />
<div style="float: left;">
Page
<asp:Label ID="lblPageIndex" runat="server" Text="Label" />
of
<asp:Label ID="lblTotalPage" runat="server" />
(<asp:Label ID="lblTotal" runat="server" />
items)
<div class="dvPager">
<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>
</div>
</div>
</div>
VB.Net
<div style="width: 450px">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="Item" Width="450px">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="Id" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="CompanyName" HeaderText="Company" />
</Columns>
</asp:GridView>
<br />
<div style="float: left;">
Page
<asp:Label ID="lblPageIndex" runat="server" Text="Label" />
of
<asp:Label ID="lblTotalPage" runat="server" />
(<asp:Label ID="lblTotal" runat="server" />
items)
<div class="dvPager">
<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>
</div>
</div>
</div>
CSS
<style type="text/css">
body {
font-family: Arial;
font-size: 10pt;
}
.Item {
border: 1px solid #ccc;
}
.Item th {
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
.Item th, .Item td {
padding: 5px;
border-color: #ccc;
}
.page_enabled, .page_disabled {
display: inline-block;
height: 25px;
min-width: 30px;
line-height: 25px;
text-align: center;
text-decoration: none;
border: 1px solid #ccc;
}
.page_disabled {
background-color: #007BFF;
color: white;
}
.page_enabled {
background-color: white;
color: #007BFF !important;
}
.dvPager {
border: 1px solid #ccc;
font-size: 0;
float: right;
border-radius: 4px;
}
.dvPager a {
font-size: 15px;
}
</style>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
private int PageSize = 10;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GetCustomersPageWise(1);
}
}
private void GetCustomersPageWise(int pageIndex)
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("Customers_GetCustomersPaging", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
GridView1.DataSource = sdr;
GridView1.DataBind();
con.Close();
int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
this.PopulatePager(recordCount, pageIndex);
}
}
}
}
protected void Page_Changed(object sender, EventArgs e)
{
int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
this.GetCustomersPageWise(pageIndex);
}
private void PopulatePager(int recordCount, int currentPage)
{
double dblPageCount = (double)((decimal)recordCount / (decimal)PageSize);
int pageCount = (int)Math.Ceiling(dblPageCount);
List<ListItem> pages = new List<ListItem>();
if (pageCount > 0)
{
if (currentPage != 1)
{
pages.Add(new ListItem("Prev", (currentPage - 1).ToString()));
}
if (pageCount < 4)
{
for (int i = 1; i <= pageCount; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
}
else if (currentPage < 4)
{
for (int i = 1; i <= 4; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
pages.Add(new ListItem("...", (currentPage).ToString(), false));
}
else if (currentPage > pageCount - 4)
{
pages.Add(new ListItem("...", (currentPage).ToString(), false));
for (int i = currentPage - 1; i <= pageCount; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
}
else
{
pages.Add(new ListItem("...", (currentPage).ToString(), false));
for (int i = currentPage - 2; i <= currentPage + 2; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
pages.Add(new ListItem("...", (currentPage).ToString(), false));
}
if (currentPage != pageCount)
{
pages.Add(new ListItem("Next", (currentPage + 1).ToString()));
}
}
rptPager.DataSource = pages;
rptPager.DataBind();
lblPageIndex.Text = currentPage.ToString();
lblTotalPage.Text = ((recordCount / PageSize) + ((recordCount % PageSize) > 0 ? 1 : 0)).ToString();
lblTotal.Text = recordCount.ToString();
}
VB.Net
Private PageSize As Integer = 10
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.GetCustomersPageWise(1)
End If
End Sub
Private Sub GetCustomersPageWise(ByVal pageIndex As Integer)
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("Customers_GetCustomersPaging", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@PageIndex", pageIndex)
cmd.Parameters.AddWithValue("@PageSize", PageSize)
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4)
cmd.Parameters("@RecordCount").Direction = ParameterDirection.Output
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
GridView1.DataSource = sdr
GridView1.DataBind()
con.Close()
Dim recordCount As Integer = Convert.ToInt32(cmd.Parameters("@RecordCount").Value)
Me.PopulatePager(recordCount, pageIndex)
End Using
End Using
End Using
End Sub
Protected Sub Page_Changed(ByVal sender As Object, ByVal e As EventArgs)
Dim pageIndex As Integer = Integer.Parse((TryCast(sender, LinkButton)).CommandArgument)
Me.GetCustomersPageWise(pageIndex)
End Sub
Private Sub PopulatePager(ByVal recordCount As Integer, ByVal currentPage As Integer)
Dim dblPageCount As Double = CDbl((CDec(recordCount) / CDec(PageSize)))
Dim pageCount As Integer = CInt(Math.Ceiling(dblPageCount))
Dim pages As List(Of ListItem) = New List(Of ListItem)()
If pageCount > 0 Then
If currentPage <> 1 Then
pages.Add(New ListItem("Prev", (currentPage - 1).ToString()))
End If
If pageCount < 4 Then
For i As Integer = 1 To pageCount
pages.Add(New ListItem(i.ToString(), i.ToString(), i <> currentPage))
Next
ElseIf currentPage < 4 Then
For i As Integer = 1 To 4
pages.Add(New ListItem(i.ToString(), i.ToString(), i <> currentPage))
Next
pages.Add(New ListItem("...", (currentPage).ToString(), False))
ElseIf currentPage > pageCount - 4 Then
pages.Add(New ListItem("...", (currentPage).ToString(), False))
For i As Integer = currentPage - 1 To pageCount
pages.Add(New ListItem(i.ToString(), i.ToString(), i <> currentPage))
Next
Else
pages.Add(New ListItem("...", (currentPage).ToString(), False))
For i As Integer = currentPage - 2 To currentPage + 2
pages.Add(New ListItem(i.ToString(), i.ToString(), i <> currentPage))
Next
pages.Add(New ListItem("...", (currentPage).ToString(), False))
End If
If currentPage <> pageCount Then
pages.Add(New ListItem("Next", (currentPage + 1).ToString()))
End If
End If
rptPager.DataSource = pages
rptPager.DataBind()
lblPageIndex.Text = currentPage.ToString()
lblTotalPage.Text = (Math.Round(recordCount / PageSize) + (If((recordCount Mod PageSize) > 0, 1, 0))).ToString()
lblTotal.Text = recordCount.ToString()
End Sub
Screenshot