See the code below, this code is using for pagination in my project. but i need to display page numbers in url.
please any one help me
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class PagerControl : System.Web.UI.UserControl
{
public event EventHandler PagerControl_PageIndexChanged;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (PageMode == LinkType.LinkButton)
{
CreateLinkButtonPagination();
}
}
}
#region Public Properties
public int IndexDisplayOffset
{
get
{
return ViewState["IndexDisplayOffset"] == null ? 1 : (int)ViewState["IndexDisplayOffset"];
}
set { ViewState["IndexDisplayOffset"] = value; }
}
public LinkType PageMode
{
get
{
return ViewState["PageMode"] == null ? LinkType.HyperLink : (LinkType)ViewState["PageMode"];
}
set { ViewState["PageMode"] = value; }
}
public int TotalItems
{
get { return ViewState["TotalItems"] == null ? 0 : (int)ViewState["TotalItems"]; }
set { ViewState["TotalItems"] = value; }
}
public int PageSize
{
get { return ViewState["PageSize"] == null ? 10 : (int)ViewState["PageSize"]; }
set { ViewState["PageSize"] = value; }
}
public int CurrentPageIndex
{
get { return _CurrentPageIndex + 1; }
set { _CurrentPageIndex = value - 1; }
}
private int _CurrentPageIndex
{
get { return ViewState["CurrentPageIndex"] == null ? 0 : (int)ViewState["CurrentPageIndex"]; }
set { ViewState["CurrentPageIndex"] = value; }
}
public int DisplayEntriesCount
{
get
{
return ViewState["DisplayEntriesCount"] == null ? 10 : (int)ViewState["DisplayEntriesCount"];
}
set { ViewState["DisplayEntriesCount"] = value; }
}
public int EdgeEntriesCount
{
get
{
return ViewState["EdgeEntriesCount"] == null ? 2 : (int)ViewState["EdgeEntriesCount"];
}
set { ViewState["EdgeEntriesCount"] = value; }
}
public string TargetLink
{
get
{
return ViewState["TargetLink"] == null ? "?p=" : ViewState["TargetLink"].ToString();
}
set { ViewState["TargetLink"] = value; }
}
public string TargetLinkFormatString
{
get
{
return ViewState["TargetLinkFormat"] == null ? string.Empty : ViewState["TargetLinkFormat"].ToString();
}
set { ViewState["TargetLinkFormat"] = value; }
}
public string PreviousPageText
{
get
{
return ViewState["PreviousPageText"] == null ? "« prev" : ViewState["PreviousPageText"].ToString();
}
set { ViewState["PreviousPageText"] = value; }
}
public string NextPageText
{
get
{
return ViewState["NextPageText"] == null ? "next »" : ViewState["NextPageText"].ToString();
}
set { ViewState["NextPageText"] = value; }
}
public string EllipseText
{
get
{
return ViewState["EllipseText"] == null ? "..." : ViewState["EllipseText"].ToString();
}
set { ViewState["EllipseText"] = value; }
}
public string CssClass
{
get { return pnlPager.Attributes["class"]; }
set { pnlPager.Attributes["class"] = value; }
}
#endregion
#region enums
public enum LinkType
{
HyperLink, LinkButton
}
#endregion
#region Public Functions
public void BindDataWithPaging(Control bindControl, DataTable data)
{
DataView dv = data.DefaultView;
TotalItems = data.Rows.Count;
PagedDataSource dsP = new PagedDataSource();
dsP.AllowPaging = true;
dsP.PageSize = PageSize;
dsP.DataSource = dv;
if (_CurrentPageIndex > dsP.PageCount - 1)
_CurrentPageIndex = dsP.PageCount - 1;
else if(_CurrentPageIndex < 0)
_CurrentPageIndex = 0;
dsP.CurrentPageIndex = _CurrentPageIndex;
if (bindControl is DataList)
((DataList)bindControl).DataSource = dsP;
else if (bindControl is Repeater)
((Repeater)bindControl).DataSource = dsP;
else if (bindControl is DataGrid)
((DataGrid)bindControl).DataSource = dsP;
else if (bindControl is GridView)
((GridView)bindControl).DataSource = dsP;
bindControl.DataBind();
CreatePagination();
}
public void CreatePagination()
{
if (TotalPages() > 1)
{
this.Visible = true;
if (PageMode == LinkType.HyperLink)
CreateHyperLinkPagination();
else
CreateLinkButtonPagination();
}
else
{
this.Visible = false;
}
}
public int TotalPages()
{
return int.Parse(Math.Ceiling(decimal.Parse(TotalItems.ToString())
/ decimal.Parse(PageSize.ToString())).ToString());
}
#endregion
#region CreateHyperLinkPagination
private void CreateHyperLinkPagination()
{
string ellipses = "<li class=\"plain\">" + EllipseText + "</li>";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
int ne_half = int.Parse(Math.Ceiling(decimal.Parse(DisplayEntriesCount.ToString()) / 2).ToString());
int np = TotalPages();
int upper_limit = np - DisplayEntriesCount;
int start = _CurrentPageIndex > ne_half ? Math.Max(Math.Min(_CurrentPageIndex - ne_half, upper_limit), 0) : 0;
int end = _CurrentPageIndex > ne_half ? Math.Min(_CurrentPageIndex + ne_half, np) : Math.Min(DisplayEntriesCount, np);
if (PreviousPageText.Length > 0)
{
sb.Append(CreateLink(_CurrentPageIndex - 1, PreviousPageText, _CurrentPageIndex == 0 ? "disabled" : "")).Append(System.Environment.NewLine);
}
if (start > 0 && EdgeEntriesCount > 0)
{
int edgeEnd = Math.Min(EdgeEntriesCount, start);
for (int i = 0; i < edgeEnd; i++)
{
sb.Append(CreateLink(i, (i + 1).ToString(), "")).Append(System.Environment.NewLine);
}
if (EdgeEntriesCount < start && EllipseText.Length > 0)
{
sb.Append(ellipses).Append(System.Environment.NewLine);
}
}
for (int i = start; i < end; i++)
{
sb.Append(CreateLink(i, (i + 1).ToString(), "")).Append(System.Environment.NewLine);
}
if (end < np && EdgeEntriesCount > 0)
{
if (np - EdgeEntriesCount > end && EllipseText.Length > 0)
{
sb.Append(ellipses).Append(System.Environment.NewLine);
}
int begin = Math.Max(np - EdgeEntriesCount, end);
for (int i = begin; i < np; i++)
{
sb.Append(CreateLink(i, (i + 1).ToString(), "")).Append(System.Environment.NewLine);
}
}
if (NextPageText.Length > 0)
{
sb.Append(CreateLink(_CurrentPageIndex + 1, NextPageText, _CurrentPageIndex == end - 1 ? "disabled" : "")).Append(System.Environment.NewLine);
}
pnlPager.InnerHtml = sb.ToString();
}
string CreateLink(int pageNumber, string displayText, string className)
{
string link = "";
int np = TotalPages();
pageNumber = pageNumber < 0 ? 0 : (pageNumber < np ? pageNumber : np - 1);
if (pageNumber == _CurrentPageIndex)
{
link = string.Format("<li class='{0}'>{1}</li>"
, className.Length > 0 ? className : "current"
, displayText);
}
else
{
if (TargetLinkFormatString.Length > 0)
{
link = string.Format(TargetLinkFormatString, pageNumber+IndexDisplayOffset);
link = string.Format("<li><a href='{0}'>{1}</a></li>", link, displayText);
}
else
link = string.Format("<li><a href='{0}{1}'>{2}</a></li>", TargetLink, pageNumber+IndexDisplayOffset, displayText);
}
return link;
}
#endregion
#region CreateLinkButtonPagination
private void CreateLinkButtonPagination()
{
pnlPager.Controls.Clear();
int ne_half = int.Parse(Math.Ceiling(decimal.Parse(DisplayEntriesCount.ToString()) / 2).ToString());
int np = TotalPages();
int upper_limit = np - DisplayEntriesCount;
int start = _CurrentPageIndex > ne_half ? Math.Max(Math.Min(_CurrentPageIndex - ne_half, upper_limit), 0) : 0;
int end = _CurrentPageIndex > ne_half ? Math.Min(_CurrentPageIndex + ne_half, np) : Math.Min(DisplayEntriesCount, np);
if (PreviousPageText.Length > 0)
CreateButton(_CurrentPageIndex - 1, PreviousPageText, _CurrentPageIndex == 0 ? "disabled" : "");
if (start > 0 && EdgeEntriesCount > 0)
{
int edgeEnd = Math.Min(EdgeEntriesCount, start);
for (int i = 0; i < edgeEnd; i++)
{
CreateButton(i, (i + 1).ToString(), "");
}
if (EdgeEntriesCount < start && EllipseText.Length > 0)
{
CreateLabel(EllipseText, "plain");
}
}
for (int i = start; i < end; i++)
{
CreateButton(i, (i + 1).ToString(), "");
}
if (end < np && EdgeEntriesCount > 0)
{
if (np - EdgeEntriesCount > end && EllipseText.Length > 0)
{
CreateLabel(EllipseText, "plain");
}
int begin = Math.Max(np - EdgeEntriesCount, end);
for (int i = begin; i < np; i++)
{
CreateButton(i, (i + 1).ToString(), "");
}
}
if (NextPageText.Length > 0)
CreateButton(_CurrentPageIndex + 1, NextPageText, _CurrentPageIndex == end - 1 ? "disabled" : "");
}
private void CreateButton(int pageNumber, string displayText, string className)
{
int np = TotalPages();
pageNumber = pageNumber < 0 ? 0 : (pageNumber < np ? pageNumber : np - 1);
if (pageNumber == _CurrentPageIndex)
{
CreateLabel(displayText, className.Length > 0 ? className : "current");
}
else
{
CreateLinkButton(displayText, pageNumber);
}
}
private void CreateLinkButton(string title, int index)
{
Literal litNewLine = new Literal();
litNewLine.Text = System.Environment.NewLine;
pnlPager.Controls.Add(litNewLine);
LinkButton lnk = new LinkButton();
if (title == PreviousPageText)
lnk.ID = "-1";
else if (title == NextPageText)
lnk.ID = TotalItems.ToString();
else
lnk.ID = index.ToString();
lnk.Text = title;
lnk.CommandArgument = index.ToString();
lnk.Click += new EventHandler(lnkPager_Click);
HtmlGenericControl li = new HtmlGenericControl("li");
li.Controls.Add(lnk);
pnlPager.Controls.Add(li);
}
private void CreateLabel(string title, string cssClass)
{
Literal litNewLine = new Literal();
litNewLine.Text = System.Environment.NewLine;
pnlPager.Controls.Add(litNewLine);
Label lbl = new Label();
lbl.ID = System.Guid.NewGuid().ToString("N");
lbl.Text = title;
HtmlGenericControl li = new HtmlGenericControl("li");
li.Attributes.Add("class", cssClass);
li.Controls.Add(lbl);
pnlPager.Controls.Add(li);
}
protected void lnkPager_Click(object sender, EventArgs e)
{
LinkButton lnk = (LinkButton)sender;
_CurrentPageIndex = int.Parse(lnk.CommandArgument);
if (PagerControl_PageIndexChanged != null)
PagerControl_PageIndexChanged(this, e);
}
#endregion
}