ASP.Netページングコントロールソース

11445 ワード

特性:、datagridを支持して、datalistなどの分種のデータはコントロールを表示して、理論の上でdatasourceのコントロールがすべてできる限り、url方式の分ページを支持して、urlの分ページの各ページはすべて検索エンジンに捜し出すことができて、しかもurlの書き換えを支持して、コントロールのスタイルはカスタマイズすることができて、完全にパッケージして、プログラムを使ったことがあってただ1行のコードを書くだけです
 
  
/******************************************************************
** :Pager.cs
** :
** :2005 7 27
** :
** :
** :
**
**----------------------------------------------------------------
******************************************************************/

using System;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;

namespace MyCompany.Controls
{
///
///
///

public class Pager : Label, INamingContainer
{
#region
HttpContext context = HttpContext.Current;
Label countLabel;
HyperLink previousButton;
HyperLink nextButton;
HyperLink firstButton;
HyperLink lastButton;
HyperLink[] pagingLinkButtons;

public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}

protected override void CreateChildControls()
{
Controls.Clear();

AddCountLabel();

AddPageButtons();

AddPreviousNextHyperLinks();

AddFirstLastHyperLinks();
}
#endregion

#region
protected override void Render(HtmlTextWriter writer)
{
// 1
// int totalPages = CalculateTotalPages();
//
// if (totalPages <= 1)
// return;

AddAttributesToRender(writer);

writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass, false);

RenderCountLabel(writer);

RenderFirst(writer);

RenderPrevious(writer);

RenderPagingButtons(writer);

RenderNext(writer);

RenderLast(writer);
}

void RenderCountLabel(HtmlTextWriter writer)
{
countLabel.RenderControl(writer);

LiteralControl l = new LiteralControl(" ");
l.RenderControl(writer);
}

void RenderFirst (HtmlTextWriter writer)
{
firstButton.RenderControl(writer);

LiteralControl l = new LiteralControl(" ");
l.RenderControl(writer);
}

void RenderLast (HtmlTextWriter writer)
{
lastButton.RenderControl(writer);

LiteralControl l = new LiteralControl(" ");
l.RenderControl(writer);
}

void RenderPrevious (HtmlTextWriter writer)
{
previousButton.RenderControl(writer);

LiteralControl l = new LiteralControl(" ");
l.RenderControl(writer);
}

void RenderNext(HtmlTextWriter writer)
{
nextButton.RenderControl(writer);

LiteralControl l = new LiteralControl(" ");
l.RenderControl(writer);
}

private void RenderButtonRange(int start, int end, HtmlTextWriter writer)
{
for (int i = start; i < end; i++)
{
if (PageIndex == i)
{
Literal l = new Literal();
l.Text = "" + (i + 1).ToString() + "";

l.RenderControl(writer);
}
else
{
pagingLinkButtons.RenderControl(writer);
}
if (i < (end - 1))
writer.Write(" ");
}

LiteralControl l1 = new LiteralControl(" ");
l1.RenderControl(writer);
}

private void RenderPagingButtons(HtmlTextWriter writer)
{
int totalPages;

totalPages = CalculateTotalPages();

if (totalPages <= 10)
{
RenderButtonRange(0, totalPages, writer);
}
else
{
int lowerBound = (PageIndex - 4);
int upperBount = (PageIndex + 6);

if (lowerBound <= 0)
lowerBound = 0;

if (PageIndex <= 4)
RenderButtonRange(0, 10, writer);

else if (PageIndex < (totalPages - 5))
RenderButtonRange(lowerBound, (PageIndex + 6), writer);

else if (PageIndex >= (totalPages - 5))
RenderButtonRange((totalPages - 10), totalPages, writer);
}
}

#endregion

#region
///
///
///

void AddCountLabel()
{
countLabel = new Label();
countLabel.ID = "countLabel";
countLabel.Text = string.Format(text, CalculateTotalPages().ToString("n0"));

Controls.Add(countLabel);
}

private void AddPageButtons()
{
pagingLinkButtons = new HyperLink[CalculateTotalPages()];

for (int i = 0; i < pagingLinkButtons.Length; i++)
{
pagingLinkButtons = new HyperLink();
pagingLinkButtons.EnableViewState = false;
pagingLinkButtons.Text = (i + 1).ToString();
pagingLinkButtons.ID = i.ToString();
pagingLinkButtons.CssClass = linkCss;
pagingLinkButtons.NavigateUrl = GetHrefString(i);

Controls.Add(pagingLinkButtons);
}
}

///
///
///

void AddFirstLastHyperLinks()
{
firstButton = new HyperLink();
firstButton.ID = "First";
firstButton.Text = " ";
if(PageIndex != 0 && CalculateTotalPages() > 0)
{
firstButton.NavigateUrl = GetHrefString(0);
}
else
{
firstButton.Enabled = false;
}

Controls.Add(firstButton);

lastButton = new HyperLink();
lastButton.ID = "Last";
lastButton.Text = " ";
if(PageIndex != CalculateTotalPages() - 1)
{
lastButton.NavigateUrl = GetHrefString(CalculateTotalPages() - 1);
}
else
{
lastButton.Enabled = false;
}
Controls.Add(lastButton);
}

///
///
///

void AddPreviousNextHyperLinks()
{
previousButton = new HyperLink();
previousButton.ID = "Prev";
previousButton.Text = " ";
if (HasPrevious)
{
previousButton.NavigateUrl = GetHrefString(PageIndex - 1);
}
else
{
previousButton.Enabled = false;
}
Controls.Add(previousButton);

nextButton = new HyperLink();
nextButton.ID = "Next";
nextButton.Text = " ";
if(HasNext)
{
nextButton.NavigateUrl = GetHrefString(PageIndex + 1);
}
else
{
nextButton.Enabled = false;
}
Controls.Add(nextButton);
}
#endregion

#region
private bool HasPrevious
{
get
{
if (PageIndex > 0)
return true;

return false;
}
}

private bool HasNext
{
get
{
if (PageIndex + 1 < CalculateTotalPages() )
return true;

return false;
}
}
#endregion

#region

///
///
///

///
///
private string GetHrefString(int pageIndex)
{
string url = Page.Request.RawUrl;

if (Regex.IsMatch(url, @"(\d+).shtml", RegexOptions.IgnoreCase))
{
url = Regex.Replace(url, @"(\d+).shtml", pageIndex + ".shtml");
}

if (Regex.IsMatch(url, @"PageIndex=(\d+)", RegexOptions.IgnoreCase))
{
url = Regex.Replace(url, @"PageIndex=(\d+)", "PageIndex=" + pageIndex.ToString());
}

return Page.Server.UrlPathEncode(url);
}

public int CalculateTotalPages()
{
int totalPagesAvailable;

if (TotalRecords == 0)
return 0;

totalPagesAvailable = TotalRecords / PageSize;

if ((TotalRecords % PageSize) > 0)
totalPagesAvailable++;

return totalPagesAvailable;
}

///
///
///

public int PageIndex
{
get
{
int _pageIndex = 0;

if ((!Page.IsPostBack) && (context.PageIndex > 0))
_pageIndex = context.PageIndex;
else
_pageIndex = Convert.ToInt32(ViewState["PageIndex"]);

if (_pageIndex < 0)
return 0;
else
{
if (_pageIndex > CalculateTotalPages()-1)
return CalculateTotalPages()-1;
else
return _pageIndex;
}
}
set
{
ViewState["PageIndex"] = value;
}
}

///
///
///

public int PageSize
{
get
{
int pageSize = Convert.ToInt32(ViewState["PageSize"]);

if (pageSize == 0)
{
return 10;
}

return pageSize;
}
set
{
ViewState["PageSize"] = value;
}

}

///
///
///

public int TotalRecords
{
get
{
return Convert.ToInt32(ViewState["TotalRecords"]);
}
set
{
ViewState["TotalRecords"] = value;
}
}

private string text = " {0} ";

private string linkCss = "";
///
///
///

public string LinkCss
{
get { return linkCss; }
set { linkCss = value;}
}

private string textCss = "";
///
///
///

public string TextCss
{
get { return textCss; }
set { textCss = value;}
}

public string TextFormat
{
get
{
return text;
}
set
{
text = value;
}
}

#endregion
}
}

ページには次のように書くだけです.