Study
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Security.Permissions;
using System.Drawing;
namespace PagerPro
{
[AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal)]
[DefaultPropertyAttribute("StartPage")]
[ToolboxData(@"<{0}:PagerPro runat='server'></{0}:PagerPro>")]
public class PagerPro : CompositeControl
{
#region /**PRIVATE PARAMETER**/
private LinkButton lbPrevious;
private LinkButton lbNext;
private LinkButton lbFirst;
private LinkButton lbLast;
private LinkButton[] lbPagingButtons;
#endregion
#region /**PROPERTYS**/
[Browsable(true)]
[CategoryAttribute("Appearance")]
public string Id
{
get { return ViewState["_id"] == null ? null : (string)ViewState["_id"]; }
set { ViewState["_id"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public int StartPage
{
get { return ViewState["_startPage"] == null ? 1 : (int)ViewState["_startPage"]; }
set { ViewState["_startPage"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public int PageCount
{
get { return ViewState["_pageCount"] == null ? 1 : (int)ViewState["_pageCount"]; }
set { ViewState["_pageCount"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public int RowCount
{
get { return ViewState["_rowCount"] == null ? 0 : (int)ViewState["_rowCount"]; }
set { ViewState["_rowCount"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public int CurrentPageNumeric
{
get { return ViewState["_currentPageNumeric"] == null ? 1 : (int)ViewState["_currentPageNumeric"]; }
set { ViewState["_currentPageNumeric"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public int NumericButtonCount
{
get { return ViewState["_numericButtonCount"] == null ? 10 : (int)ViewState["_numericButtonCount"]; }
set { ViewState["_numericButtonCount"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public bool ShowPrevious
{
get { return ViewState["_showPrevious"] == null ? true : (bool)ViewState["_showPrevious"]; }
set { ViewState["_showPrevious"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public bool ShowNext
{
get { return ViewState["_showNext"] == null ? true : (bool)ViewState["_showNext"]; }
set { ViewState["_showNext"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public bool ShowFirst
{
get { return ViewState["_showFirst"] == null ? true : (bool)ViewState["_showFirst"]; }
set { ViewState["_showFirst"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public bool ShowLast
{
get { return ViewState["_showLast"] == null ? true : (bool)ViewState["_showLast"]; }
set { ViewState["_showLast"] = value; }
}
#endregion
#region /**PRIVATE METHOD**/
private void AddPagingButtons()
{
int showPageCount = 0;
if (StartPage + NumericButtonCount - 1 <= PageCount)
showPageCount = NumericButtonCount;
else
showPageCount = PageCount - StartPage + 1;
lbPagingButtons = new LinkButton[showPageCount];
for (int i = 0; i < showPageCount; i++)
{
lbPagingButtons[i] = new LinkButton();
lbPagingButtons[i].ID = ((int)(StartPage + i)).ToString();
lbPagingButtons[i].Text = ((int)(StartPage + i)).ToString();
//lbPagingButtons[i].Click += delegate(object sender, EventArgs e)
//{
// string str = e.ToString();
// CurrentPageNumeric = StartPage + i;
//};
lbPagingButtons[i].Click+=new EventHandler(PagerPro_Click);
this.Controls.Add(lbPagingButtons[i]);
}
}
//private void ReAddPagingButtons()
//{
// int showPageCount = 0;
// if (StartPage + NumericButtonCount - 1 <= PageCount)
// showPageCount = NumericButtonCount;
// else
// showPageCount = PageCount - StartPage + 1;
// lbPagingButtons = new LinkButton[showPageCount];
// for (int i = 0; i < showPageCount; i++)
// {
// lbPagingButtons[i] = new LinkButton();
// lbPagingButtons[i].ID = ((int)(StartPage + i)).ToString();
// lbPagingButtons[i].Text = ((int)(StartPage + i)).ToString();
// this.Controls.Add(lbPagingButtons[i]);
// }
//}
private void RenderButtonsRange(HtmlTextWriter writer)
{
int showPageCount = 0;
if (StartPage + NumericButtonCount - 1 <= PageCount)
showPageCount = NumericButtonCount;
else
showPageCount = PageCount - StartPage + 1;
for (int i = 0; i < showPageCount; i++)
{
RenderControl(writer,lbPagingButtons[i]);
}
}
//private string CreateANumericButton(string number)
//{
// LinkButton lbNumber = new LinkButton();
// lbNumber.ID = number;
// lbNumber.Text = number;
// lbNumber.Click+=new EventHandler(lbNumber_Click);
//}
//private string ReturnNumericButton()
//{
// string strReturn = "";
// for (int i = 0; i < NumericButtonCount; i++)
// {
// int number = StartPage + i;
// if (number <= PageCount)
// strReturn += "<a href='#'>" + number.ToString() + "</a> ";
// }
// return strReturn;
//}
private void AddTdBoderStyle(HtmlTextWriter writer)
{
//writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px");
//writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "25px");
writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center");
//writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(Color.CornflowerBlue));
//writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, BorderStyle.Solid.ToString());
}
private void RenderControl(HtmlTextWriter writer, LinkButton lb)
{
AddTdBoderStyle(writer);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
lb.RenderControl(writer);
writer.RenderEndTag();
}
#endregion
#region /**EVENT**/
protected void PagerPro_Click(object sender, EventArgs e)
{
LinkButton lb=(LinkButton)sender;
int currentNumber = Convert.ToInt32(lb.ID.Trim());
CurrentPageNumeric = currentNumber;
AddPagingButtons();
}
protected void lbFirst_Click(object sender, EventArgs e)
{
StartPage = 1;
CurrentPageNumeric = 1;
AddPagingButtons();
}
protected void lbLast_Click(object sender, EventArgs e)
{
if (CurrentPageNumeric != PageCount)
{
int temp = PageCount / NumericButtonCount;
StartPage = NumericButtonCount * temp+1;
CurrentPageNumeric = PageCount;
AddPagingButtons();
}
}
protected void lbPrevious_Click(object sender, EventArgs e)
{
if (CurrentPageNumeric > StartPage)
CurrentPageNumeric--;
else if (CurrentPageNumeric == StartPage)
{
if (StartPage == 1)
return;
else
{
StartPage = CurrentPageNumeric - NumericButtonCount;
CurrentPageNumeric--;
}
}
AddPagingButtons();
}
protected void lbNext_Click(object sender, EventArgs e)
{
int currentLastNumber = StartPage + NumericButtonCount - 1;
if (currentLastNumber < PageCount)
{
if (CurrentPageNumeric < currentLastNumber)
CurrentPageNumeric++;
else if (CurrentPageNumeric == currentLastNumber)
{
StartPage = CurrentPageNumeric + 1;
CurrentPageNumeric++;
}
}
else if (currentLastNumber >= PageCount)
{
if (CurrentPageNumeric < PageCount)
CurrentPageNumeric++;
else if (CurrentPageNumeric == PageCount)
return;
}
AddPagingButtons();
}
#endregion
#region /**OVERRIDE**/
protected override void RecreateChildControls()
{
EnsureChildControls();
}
protected override void CreateChildControls()
{
this.Controls.Clear();
lbFirst = new LinkButton();
lbFirst.ID = "lbFirst";
lbFirst.Text = "<<";
lbFirst.Width = Unit.Parse("25px");
lbFirst.Font.Underline = false;
lbFirst.Click += new EventHandler(lbFirst_Click);
lbLast = new LinkButton();
lbLast.ID = "lbLast";
lbLast.Text = ">>";
lbLast.Width = Unit.Parse("25px");
lbLast.Font.Underline = false;
lbLast.Click += new EventHandler(lbLast_Click);
lbPrevious = new LinkButton();
lbPrevious.ID = "lbPrvious";
lbPrevious.Text = "<";
lbPrevious.Width = Unit.Parse("25px");
lbPrevious.Font.Underline = false;
lbPrevious.Click += new EventHandler(lbPrevious_Click);
lbNext = new LinkButton();
lbNext.ID = "lbNext";
lbNext.Text = ">";
lbNext.Width = Unit.Parse("25px");
lbNext.Font.Underline = false;
lbNext.Click += new EventHandler(lbNext_Click);
this.Controls.Add(lbFirst);
this.Controls.Add(lbLast);
this.Controls.Add(lbPrevious);
this.Controls.Add(lbNext);
AddPagingButtons();
}
protected override void Render(HtmlTextWriter writer)
{
AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "3");
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px");
writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, BorderStyle.Solid.ToString());
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write("Total Page: " + PageCount.ToString());
LiteralControl lc = new LiteralControl();
lc.Text = " ";
lc.RenderControl(writer);
writer.RenderEndTag();
if (ShowFirst == true)
{
RenderControl(writer,lbFirst);
}
if (ShowPrevious == true)
{
RenderControl(writer, lbPrevious);
}
RenderButtonsRange(writer);
if (ShowNext == true)
{
RenderControl(writer, lbNext);
}
if (ShowLast == true)
{
RenderControl(writer, lbLast);
}
writer.RenderBeginTag(HtmlTextWriterTag.Td);
//input text
writer.AddAttribute(HtmlTextWriterAttribute.Id, "txtGoToPage");
writer.AddAttribute(HtmlTextWriterAttribute.Name, "GoToPage");
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "30");
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
//input button
writer.AddAttribute(HtmlTextWriterAttribute.Id, "btnGo");
writer.AddAttribute(HtmlTextWriterAttribute.Name, "Go");
writer.AddAttribute(HtmlTextWriterAttribute.Value, "Go");
writer.AddAttribute(HtmlTextWriterAttribute.Type, "button");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "30");
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
//base.Render(writer);
}
#endregion
public PagerPro()
{ }
}
}
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Security.Permissions;
using System.Drawing;
namespace PagerPro
{
[AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal)]
[DefaultPropertyAttribute("StartPage")]
[ToolboxData(@"<{0}:PagerPro runat='server'></{0}:PagerPro>")]
public class PagerPro : CompositeControl
{
#region /**PRIVATE PARAMETER**/
private LinkButton lbPrevious;
private LinkButton lbNext;
private LinkButton lbFirst;
private LinkButton lbLast;
private LinkButton[] lbPagingButtons;
#endregion
#region /**PROPERTYS**/
[Browsable(true)]
[CategoryAttribute("Appearance")]
public string Id
{
get { return ViewState["_id"] == null ? null : (string)ViewState["_id"]; }
set { ViewState["_id"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public int StartPage
{
get { return ViewState["_startPage"] == null ? 1 : (int)ViewState["_startPage"]; }
set { ViewState["_startPage"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public int PageCount
{
get { return ViewState["_pageCount"] == null ? 1 : (int)ViewState["_pageCount"]; }
set { ViewState["_pageCount"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public int RowCount
{
get { return ViewState["_rowCount"] == null ? 0 : (int)ViewState["_rowCount"]; }
set { ViewState["_rowCount"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public int CurrentPageNumeric
{
get { return ViewState["_currentPageNumeric"] == null ? 1 : (int)ViewState["_currentPageNumeric"]; }
set { ViewState["_currentPageNumeric"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public int NumericButtonCount
{
get { return ViewState["_numericButtonCount"] == null ? 10 : (int)ViewState["_numericButtonCount"]; }
set { ViewState["_numericButtonCount"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public bool ShowPrevious
{
get { return ViewState["_showPrevious"] == null ? true : (bool)ViewState["_showPrevious"]; }
set { ViewState["_showPrevious"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public bool ShowNext
{
get { return ViewState["_showNext"] == null ? true : (bool)ViewState["_showNext"]; }
set { ViewState["_showNext"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public bool ShowFirst
{
get { return ViewState["_showFirst"] == null ? true : (bool)ViewState["_showFirst"]; }
set { ViewState["_showFirst"] = value; }
}
[Browsable(true)]
[CategoryAttribute("Appearance")]
public bool ShowLast
{
get { return ViewState["_showLast"] == null ? true : (bool)ViewState["_showLast"]; }
set { ViewState["_showLast"] = value; }
}
#endregion
#region /**PRIVATE METHOD**/
private void AddPagingButtons()
{
int showPageCount = 0;
if (StartPage + NumericButtonCount - 1 <= PageCount)
showPageCount = NumericButtonCount;
else
showPageCount = PageCount - StartPage + 1;
lbPagingButtons = new LinkButton[showPageCount];
for (int i = 0; i < showPageCount; i++)
{
lbPagingButtons[i] = new LinkButton();
lbPagingButtons[i].ID = ((int)(StartPage + i)).ToString();
lbPagingButtons[i].Text = ((int)(StartPage + i)).ToString();
//lbPagingButtons[i].Click += delegate(object sender, EventArgs e)
//{
// string str = e.ToString();
// CurrentPageNumeric = StartPage + i;
//};
lbPagingButtons[i].Click+=new EventHandler(PagerPro_Click);
this.Controls.Add(lbPagingButtons[i]);
}
}
//private void ReAddPagingButtons()
//{
// int showPageCount = 0;
// if (StartPage + NumericButtonCount - 1 <= PageCount)
// showPageCount = NumericButtonCount;
// else
// showPageCount = PageCount - StartPage + 1;
// lbPagingButtons = new LinkButton[showPageCount];
// for (int i = 0; i < showPageCount; i++)
// {
// lbPagingButtons[i] = new LinkButton();
// lbPagingButtons[i].ID = ((int)(StartPage + i)).ToString();
// lbPagingButtons[i].Text = ((int)(StartPage + i)).ToString();
// this.Controls.Add(lbPagingButtons[i]);
// }
//}
private void RenderButtonsRange(HtmlTextWriter writer)
{
int showPageCount = 0;
if (StartPage + NumericButtonCount - 1 <= PageCount)
showPageCount = NumericButtonCount;
else
showPageCount = PageCount - StartPage + 1;
for (int i = 0; i < showPageCount; i++)
{
RenderControl(writer,lbPagingButtons[i]);
}
}
//private string CreateANumericButton(string number)
//{
// LinkButton lbNumber = new LinkButton();
// lbNumber.ID = number;
// lbNumber.Text = number;
// lbNumber.Click+=new EventHandler(lbNumber_Click);
//}
//private string ReturnNumericButton()
//{
// string strReturn = "";
// for (int i = 0; i < NumericButtonCount; i++)
// {
// int number = StartPage + i;
// if (number <= PageCount)
// strReturn += "<a href='#'>" + number.ToString() + "</a> ";
// }
// return strReturn;
//}
private void AddTdBoderStyle(HtmlTextWriter writer)
{
//writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px");
//writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "25px");
writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center");
//writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(Color.CornflowerBlue));
//writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, BorderStyle.Solid.ToString());
}
private void RenderControl(HtmlTextWriter writer, LinkButton lb)
{
AddTdBoderStyle(writer);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
lb.RenderControl(writer);
writer.RenderEndTag();
}
#endregion
#region /**EVENT**/
protected void PagerPro_Click(object sender, EventArgs e)
{
LinkButton lb=(LinkButton)sender;
int currentNumber = Convert.ToInt32(lb.ID.Trim());
CurrentPageNumeric = currentNumber;
AddPagingButtons();
}
protected void lbFirst_Click(object sender, EventArgs e)
{
StartPage = 1;
CurrentPageNumeric = 1;
AddPagingButtons();
}
protected void lbLast_Click(object sender, EventArgs e)
{
if (CurrentPageNumeric != PageCount)
{
int temp = PageCount / NumericButtonCount;
StartPage = NumericButtonCount * temp+1;
CurrentPageNumeric = PageCount;
AddPagingButtons();
}
}
protected void lbPrevious_Click(object sender, EventArgs e)
{
if (CurrentPageNumeric > StartPage)
CurrentPageNumeric--;
else if (CurrentPageNumeric == StartPage)
{
if (StartPage == 1)
return;
else
{
StartPage = CurrentPageNumeric - NumericButtonCount;
CurrentPageNumeric--;
}
}
AddPagingButtons();
}
protected void lbNext_Click(object sender, EventArgs e)
{
int currentLastNumber = StartPage + NumericButtonCount - 1;
if (currentLastNumber < PageCount)
{
if (CurrentPageNumeric < currentLastNumber)
CurrentPageNumeric++;
else if (CurrentPageNumeric == currentLastNumber)
{
StartPage = CurrentPageNumeric + 1;
CurrentPageNumeric++;
}
}
else if (currentLastNumber >= PageCount)
{
if (CurrentPageNumeric < PageCount)
CurrentPageNumeric++;
else if (CurrentPageNumeric == PageCount)
return;
}
AddPagingButtons();
}
#endregion
#region /**OVERRIDE**/
protected override void RecreateChildControls()
{
EnsureChildControls();
}
protected override void CreateChildControls()
{
this.Controls.Clear();
lbFirst = new LinkButton();
lbFirst.ID = "lbFirst";
lbFirst.Text = "<<";
lbFirst.Width = Unit.Parse("25px");
lbFirst.Font.Underline = false;
lbFirst.Click += new EventHandler(lbFirst_Click);
lbLast = new LinkButton();
lbLast.ID = "lbLast";
lbLast.Text = ">>";
lbLast.Width = Unit.Parse("25px");
lbLast.Font.Underline = false;
lbLast.Click += new EventHandler(lbLast_Click);
lbPrevious = new LinkButton();
lbPrevious.ID = "lbPrvious";
lbPrevious.Text = "<";
lbPrevious.Width = Unit.Parse("25px");
lbPrevious.Font.Underline = false;
lbPrevious.Click += new EventHandler(lbPrevious_Click);
lbNext = new LinkButton();
lbNext.ID = "lbNext";
lbNext.Text = ">";
lbNext.Width = Unit.Parse("25px");
lbNext.Font.Underline = false;
lbNext.Click += new EventHandler(lbNext_Click);
this.Controls.Add(lbFirst);
this.Controls.Add(lbLast);
this.Controls.Add(lbPrevious);
this.Controls.Add(lbNext);
AddPagingButtons();
}
protected override void Render(HtmlTextWriter writer)
{
AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "3");
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px");
writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, BorderStyle.Solid.ToString());
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write("Total Page: " + PageCount.ToString());
LiteralControl lc = new LiteralControl();
lc.Text = " ";
lc.RenderControl(writer);
writer.RenderEndTag();
if (ShowFirst == true)
{
RenderControl(writer,lbFirst);
}
if (ShowPrevious == true)
{
RenderControl(writer, lbPrevious);
}
RenderButtonsRange(writer);
if (ShowNext == true)
{
RenderControl(writer, lbNext);
}
if (ShowLast == true)
{
RenderControl(writer, lbLast);
}
writer.RenderBeginTag(HtmlTextWriterTag.Td);
//input text
writer.AddAttribute(HtmlTextWriterAttribute.Id, "txtGoToPage");
writer.AddAttribute(HtmlTextWriterAttribute.Name, "GoToPage");
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "30");
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
//input button
writer.AddAttribute(HtmlTextWriterAttribute.Id, "btnGo");
writer.AddAttribute(HtmlTextWriterAttribute.Name, "Go");
writer.AddAttribute(HtmlTextWriterAttribute.Value, "Go");
writer.AddAttribute(HtmlTextWriterAttribute.Type, "button");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "30");
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
//base.Render(writer);
}
#endregion
public PagerPro()
{ }
}
}