C# 代码
[DefaultProperty("Text")]
[ToolboxData("<{0}:splitpage runat=server></{0}:splitpage>")]
public class splitpage : WebControl,IPostBackEventHandler
{
private int _SelectPageCount = 10;
[Bindable(true)]
[Category("SelectPageCount")]
[DefaultValue("10")]
[Localizable(true)]
public int SelectPageCount {
get {
return _SelectPageCount;
}
set { _SelectPageCount = value; }
}
private int _PageIndex = 1;
[Bindable(true)]
[Category("PageIndex")]
[DefaultValue("10")]
[Localizable(true)]
public int PageIndex
{
get
{
return _PageIndex;
}
set { _PageIndex = value; }
}
private int _PageCount = 1;
[Bindable(true)]
[Category("PageCount")]
[DefaultValue("1")]
[Localizable(true)]
public int PageCount
{
get
{
return _PageCount;
}
set { _PageCount = value; }
}
string _pageindexCss = "PageIndexCSS";
[Bindable(true)]
[Category("PageIndexCss")]
[DefaultValue("1")]
[Localizable(true)]
public string PageIndexCss
{
get { return _pageindexCss; }
set { _pageindexCss = value; }
}
string _CurPageindexCss = "PageCurIndexCSS";
[Bindable(true)]
[Category("CurPageIndexCss")]
[DefaultValue("1")]
[Localizable(true)]
public string CurPageIndexCss
{
get { return _CurPageindexCss; }
set { _CurPageindexCss = value; }
}
public delegate void DelegatePageIndexChange(object sender,int pageIndex,int pageCount);
public event DelegatePageIndexChange PageIndexChangeHanlder;
protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Div; } }
protected override void RenderContents(HtmlTextWriter output)
{
if (CurPageIndexCss == "PageCurIndexCSS" || PageIndexCss == "PageIndexCSS")
{
output.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
output.RenderBeginTag(HtmlTextWriterTag.Style);
output.Write("a.PageIndexCSS{padding: 2px;padding-left:4px;padding-right:4px;background: #fff;border: 1px solid #9AAFE5;text-decoration: none;color: #2452ac;margin-right:6px;" +
"cursor: pointer;}a.PageIndexCSS:link{color: #2452ac;text-decoration: none;cursor: pointer;}a.PageIndexCSS:visited{color: #2452ac;text-decoration: none;cursor: pointer;}a.PageIndexCSS:active" +
"{color: #2452ac;text-decoration: underline;cursor: pointer;}a.PageIndexCSS:hover{border: 1px solid #2452ac;color: black;text-decoration: underline;cursor: pointer;}a.PageCurIndexCSS{padding: 2px;" +
"padding-left:4px;padding-right:4px;border: 1px solid #2452ac;background-color: #2452ac;text-decoration: none;color: #fff;margin-right:6px;}");
output.RenderEndTag();
}
var showPagesList = new List<int>();
showPagesList.Add(1);
if (PageCount > 1) showPagesList.Add(2);
int showFirst = PageIndex - SelectPageCount / 2 + 1;
int showLast = PageIndex + SelectPageCount / 2;
if (showFirst < 0) showLast = showLast - showFirst;
if (showLast > PageCount) showLast = PageCount;
for (int i = showFirst; i < showLast; i++)
{
if (!showPagesList.Contains(i) && i > 0) showPagesList.Add(i);
}
if (PageCount - 1 > 1 && !showPagesList.Contains(PageCount - 1)) showPagesList.Add(PageCount - 1);
if (PageCount > 1 && !showPagesList.Contains(PageCount)) showPagesList.Add(PageCount);
int preIndex = PageIndex > 1 ? PageIndex - 1 : 0;
if (preIndex > 0)
{
output.AddAttribute(HtmlTextWriterAttribute.Class, PageIndexCss);
output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, "__sp_TurnPage_" + preIndex + "_" + PageCount));
output.RenderBeginTag(HtmlTextWriterTag.A);
output.Write("上一页");
output.RenderEndTag();
}
int showedindex = 0;
foreach (int index in showPagesList)
{
if (index - showedindex > 1)
{
output.RenderBeginTag(HtmlTextWriterTag.Span);
output.Write(" ... ");
output.RenderEndTag();
}
output.AddAttribute(HtmlTextWriterAttribute.Class,index==PageIndex?CurPageIndexCss:PageIndexCss);
if (index != PageIndex) output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, "__sp_TurnPage_" + index + "_" + PageCount));
output.RenderBeginTag(HtmlTextWriterTag.A);
output.Write(index);
output.RenderEndTag();
showedindex = index;
}
int nextIndex = PageIndex < PageCount ? PageIndex + 1 : 0;
if (nextIndex > 0)
{
output.AddAttribute(HtmlTextWriterAttribute.Class, PageIndexCss);
output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, "__sp_TurnPage_" + nextIndex + "_" + PageCount));
output.RenderBeginTag(HtmlTextWriterTag.A);
output.Write("下一页");
output.RenderEndTag();
}
}
public void RaisePostBackEvent(string eventArgument)
{
string[] pcs = eventArgument.Split('_');
PageIndex = int.Parse(pcs[4]);
PageCount = int.Parse(pcs[5]);
if (PageIndexChangeHanlder != null)
{
PageIndexChangeHanlder(this, PageIndex, PageCount);
}
}
}