创建自定义服务器控件:
分页控件:
分页控件属性:
页面级分页代码:
.CS
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace TianCai.Infrastructure.WebControls
{
[ToolboxData("<{0}:PageBar runat=\"server\"></{0}:PageBar>")]
public class PageBar : WebControl, IPostBackEventHandler
{
[Category("Layout")]
[DefaultValue(0)]
public int CellPadding
{
get
{
if (this.ViewState[string.Concat(this.UniqueID, "CellPadding")] == null)
{
return 0;
}
return (int)this.ViewState[string.Concat(this.UniqueID, "CellPadding")];
}
set
{
this.ViewState[string.Concat(this.UniqueID, "CellPadding")] = value;
}
}
[Category("Layout")]
[DefaultValue(0)]
public int CellSpacing
{
get
{
if (this.ViewState[string.Concat(this.UniqueID, "CellSpacing")] == null)
{
return 0;
}
return (int)this.ViewState[string.Concat(this.UniqueID, "CellSpacing")];
}
set
{
this.ViewState[string.Concat(this.UniqueID, "CellSpacing")] = value;
}
}
[Browsable(false)]
[Category("Data")]
public int CurrentPage
{
get
{
int pageCount = (this.ViewState[string.Concat(this.UniqueID, "CurrentPage")] == null ? 1 : (int)this.ViewState[string.Concat(this.UniqueID, "CurrentPage")]);
if (pageCount > this.PageCount)
{
pageCount = this.PageCount;
this.CurrentPage = pageCount;
}
if (pageCount <= 1)
{
return 1;
}
return pageCount;
}
set
{
this.ViewState[string.Concat(this.UniqueID, "CurrentPage")] = value;
}
}
[Category("Navigation")]
[DefaultValue("首页")]
public string FirstPageText
{
get
{
if (this.ViewState[string.Concat(this.UniqueID, "FirstPageText")] == null)
{
return "首页";
}
return (string)this.ViewState[string.Concat(this.UniqueID, "FirstPageText")];
}
set
{
this.ViewState[string.Concat(this.UniqueID, "FirstPageText")] = value;
}
}
[Category("Layout")]
public override Unit Height
{
get
{
if (this.ViewState[string.Concat(this.UniqueID, "Height")] == null)
{
return Unit.Pixel(20);
}
return (Unit)this.ViewState[string.Concat(this.UniqueID, "Height")];
}
set
{
this.ViewState[string.Concat(this.UniqueID, "Height")] = value;
}
}
[Category("Navigation")]
[DefaultValue("末页")]
public string LastPageText
{
get
{
if (this.ViewState[string.Concat(this.UniqueID, "LastPageText")] == null)
{
return "末页";
}
return (string)this.ViewState[string.Concat(this.UniqueID, "LastPageText")];
}
set
{
this.ViewState[string.Concat(this.UniqueID, "LastPageText")] = value;
}
}
[Category("Navigation")]
[DefaultValue("下一页")]
public string NextPageText
{
get
{
if (this.ViewState[string.Concat(this.UniqueID, "NextPageText")] == null)
{
return "下一页";
}
return (string)this.ViewState[string.Concat(this.UniqueID, "NextPageText")];
}
set
{
this.ViewState[string.Concat(this.UniqueID, "NextPageText")] = value;
}
}
[Browsable(false)]
public int PageCount
{
get
{
if (this.RecordCount == 0)
{
return 0;
}
return (int)Math.Ceiling((double)this.RecordCount / (double)this.PageSize);
}
}
[Category("Navigation")]
[DefaultValue("CurrentPage")]
public string PageParamName
{
get
{
if (this.ViewState[string.Concat(this.UniqueID, "PageParamName")] == null)
{
return "CurrentPage";
}
return (string)this.ViewState[string.Concat(this.UniqueID, "PageParamName")];
}
set
{
this.ViewState[string.Concat(this.UniqueID, "PageParamName")] = value;
}
}
[Category("Appearance")]
public string PagerCssClass
{
get
{
if (this.ViewState[string.Concat(this.UniqueID, "PagerCssClass")] == null)
{
return string.Empty;
}
return (string)this.ViewState[string.Concat(this.UniqueID, "PagerCssClass")];
}
set
{
this.ViewState[string.Concat(this.UniqueID, "PagerCssClass")] = value;
}
}
[Category("Data")]
[DefaultValue(12)]
public int PageSize
{
get
{
int num = (this.ViewState[string.Concat(this.UniqueID, "PageSize")] == null ? 12 : (int)this.ViewState[string.Concat(this.UniqueID, "PageSize")]);
if (num <= 0)
{
return 0;
}
return num;
}
set
{
this.ViewState[string.Concat(this.UniqueID, "PageSize")] = value;
}
}
[Category("Navigation")]
[DefaultValue("上一页")]
public string PrePageText
{
get
{
if (this.ViewState[string.Concat(this.UniqueID, "PrePageText")] == null)
{
return "上一页";
}
return (string)this.ViewState[string.Concat(this.UniqueID, "PrePageText")];
}
set
{
this.ViewState[string.Concat(this.UniqueID, "PrePageText")] = value;
}
}
[Browsable(false)]
[Category("Data")]
public int RecordCount
{
get
{
if (this.ViewState[string.Concat(this.UniqueID, "RecordCount")] == null)
{
return 0;
}
return (int)this.ViewState[string.Concat(this.UniqueID, "RecordCount")];
}
set
{
this.ViewState[string.Concat(this.UniqueID, "RecordCount")] = value;
}
}
[Category("Appearance")]
public string TableCellCssClass
{
get
{
if (this.ViewState[string.Concat(this.UniqueID, "TableCellCssClass")] == null)
{
return string.Empty;
}
return (string)this.ViewState[string.Concat(this.UniqueID, "TableCellCssClass")];
}
set
{
this.ViewState[string.Concat(this.UniqueID, "TableCellCssClass")] = value;
}
}
[Category("Appearance")]
public string TableCssClass
{
get
{
if (this.ViewState[string.Concat(this.UniqueID, "TableCssClass")] == null)
{
return string.Empty;
}
return (string)this.ViewState[string.Concat(this.UniqueID, "TableCssClass")];
}
set
{
this.ViewState[string.Concat(this.UniqueID, "TableCssClass")] = value;
}
}
[Category("Layout")]
public override Unit Width
{
get
{
if (this.ViewState[string.Concat(this.UniqueID, "Width")] == null)
{
return Unit.Percentage(100);
}
return (Unit)this.ViewState[string.Concat(this.UniqueID, "Width")];
}
set
{
this.ViewState[string.Concat(this.UniqueID, "Width")] = value;
}
}
public PageBar()
{
}
private void GetHTML(HtmlTextWriter writer)
{
string[] pagerLinks = this.GetPagerLinks();
writer.Write("<script language='javascript'>\n");
writer.Write(string.Concat("function ", this.UniqueID, "ChangePage(pageCount,page)\n"));
writer.Write("{");
//writer.Write("if (page == pageCount) return;\n");
writer.Write("if (page > pageCount || page < 1 || isNaN(page))");
writer.Write("\r\n {\r\n alert_s('请输入正确的页码!');\r\n return;\r\n } \t\t\t \t\t\t \r\n ");
writer.Write(string.Concat("__doPostBack('", this.UniqueID, "',page)"));
writer.Write("\r\n }\r\n </script>\r\n ");
//隐藏栏位【当前页】
HtmlInputHidden htmlInputHidden = new HtmlInputHidden();
htmlInputHidden.Attributes.Add("name", "currentPage");
htmlInputHidden.Value = this.CurrentPage.ToString();
HtmlTable htmlTable = new HtmlTable()
{
Width = this.Width.ToString(),
Height = this.Height.ToString(),
CellPadding = this.CellPadding,
CellSpacing = this.CellSpacing
};
htmlTable.Attributes.Add("class", this.TableCssClass);
HtmlTableRow htmlTableRow = new HtmlTableRow();
HtmlTableCell htmlTableCell = new HtmlTableCell()
{
Align = "left",
Width = "100"
};
if (this.TableCellCssClass.Length != 0)
{
htmlTableCell.Attributes.Add("class", this.TableCellCssClass);
}
ControlCollection controls = htmlTableCell.Controls;
int recordCount = this.RecordCount;
controls.Add(new LiteralControl(string.Concat(" 共 ","<i style=\"color:#056dae;font-style: normal;\">",recordCount.ToString(),"</i>"," 条数据")));
htmlTableRow.Cells.Add(htmlTableCell);
htmlTableCell = new HtmlTableCell()
{
Align = "center"
};
//if (this.TableCellCssClass.Length != 0)
//{
// htmlTableCell.Attributes.Add("class", this.TableCellCssClass);
//}
htmlTableCell.Attributes.Add("class","pageclassstylehome");
//首页
HyperLink hyperLink = new HyperLink();
//hyperLink.Attributes.Add("class", this.PagerCssClass);
hyperLink.Attributes.Add("class","pageclassstyle pageclassstylehome");
hyperLink.Attributes.Add("style","border-left:1px solid #ddd;");
if (pagerLinks[0] == null)
{
hyperLink.Enabled = false;
}
else
{
hyperLink.NavigateUrl = string.Concat("javascript:", this.Page.ClientScript.GetPostBackEventReference(this, pagerLinks[0]));
}
hyperLink.Text = this.FirstPageText;
htmlTableCell.Controls.Add(hyperLink);
//空格
htmlTableCell.Controls.Add(new LiteralControl(" "));
//上一页
hyperLink = new HyperLink()
{
ID = "prePage"
};
//hyperLink.Attributes.Add("class", this.PagerCssClass);
hyperLink.Attributes.Add("class","pageclassstyle");
if (pagerLinks[1] == null)
{
hyperLink.Enabled = false;
}
else
{
hyperLink.NavigateUrl = string.Concat("javascript:", this.Page.ClientScript.GetPostBackEventReference(this, pagerLinks[1]));
}
hyperLink.Text = this.PrePageText;
htmlTableCell.Controls.Add(hyperLink);
//空格
htmlTableCell.Controls.Add(new LiteralControl(" "));
//下一页
hyperLink = new HyperLink();
//hyperLink.Attributes.Add("class", this.PagerCssClass);
hyperLink.Attributes.Add("class","pageclassstyle");
if (pagerLinks[2] == null)
{
hyperLink.Enabled = false;
}
else
{
hyperLink.NavigateUrl = string.Concat("javascript:", this.Page.ClientScript.GetPostBackEventReference(this, pagerLinks[2]));
}
hyperLink.Text = this.NextPageText;
htmlTableCell.Controls.Add(hyperLink);
//空格
htmlTableCell.Controls.Add(new LiteralControl(" "));
//末页
hyperLink = new HyperLink();
//hyperLink.Attributes.Add("class", this.PagerCssClass);
hyperLink.Attributes.Add("class","pageclassstyle");
if (pagerLinks[3] == null)
{
hyperLink.Enabled = false;
}
else
{
hyperLink.NavigateUrl = string.Concat("javascript:", this.Page.ClientScript.GetPostBackEventReference(this, pagerLinks[3]));
}
hyperLink.Text = this.LastPageText;
htmlTableCell.Controls.Add(hyperLink);
//空格
htmlTableCell.Controls.Add(new LiteralControl(" "));
HtmlInputText htmlInputText = new HtmlInputText()
{
ID = string.Concat(this.UniqueID, "GoToPage"),
Name = string.Concat(this.UniqueID, "GoToPage")
};
htmlInputText.Attributes.Add("style","width:30px; background:#f5f5f5; color:#3399d5;");
htmlInputText.Attributes.Add("class","pageclassstyle");
htmlInputText.Value = this.CurrentPage.ToString();
//转到
hyperLink = new HyperLink();
//hyperLink.Attributes.Add("class", this.PagerCssClass);
hyperLink.Attributes.Add("class","pageclassstyle");
if (this.PageCount <= 1)
{
hyperLink.Enabled = false;
}
else
{
object[] uniqueID = new object[] { "javascript:", this.UniqueID, "ChangePage(", this.PageCount, ",document.getElementById('", htmlInputText.ClientID, "').value);" };
hyperLink.NavigateUrl = string.Concat(uniqueID);
}
hyperLink.Text = "转到";
htmlTableCell.Controls.Add(hyperLink);
//页数
htmlTableCell.Controls.Add(htmlInputText);
//汉子页
htmlTableCell.Controls.Add(new LiteralControl("<a class=\"pageclassstyle\">页</a>"));
htmlTableRow.Cells.Add(htmlTableCell);
//汉子第m/n页
htmlTableCell = new HtmlTableCell()
{
Align = "right"
};
if (this.TableCellCssClass.Length != 0)
{
htmlTableCell.Attributes.Add("class", this.TableCellCssClass);
}
ControlCollection controlCollections = htmlTableCell.Controls;
object[] currentPage = new object[] { "第 ","<i style=\"color:#056dae;font-style: normal;\">", this.CurrentPage, " / ", this.PageCount, "</i> 页 " };
controlCollections.Add(new LiteralControl(string.Concat(currentPage)));
htmlTableRow.Cells.Add(htmlTableCell);
htmlTable.Rows.Add(htmlTableRow);
htmlTable.RenderControl(writer);
htmlInputHidden.RenderControl(writer);
}
private string[] GetPagerLinks()
{
string str;
string str1;
string str2;
string str3;
string[] strArrays = new string[5];
int num = 1;
int num1 = 1;
if (this.RecordCount <= 0)
{
this.CurrentPage = 0;
return strArrays;
}
string pagerUrl = this.GetPagerUrl();
num = (this.CurrentPage > 1 ? this.CurrentPage - 1 : 1);
num1 = (this.CurrentPage + 1 > this.PageCount ? this.PageCount : this.CurrentPage + 1);
string[] strArrays1 = strArrays;
if (this.CurrentPage == 1)
{
str = null;
}
else
{
str = "1";
}
strArrays1[0] = str;
string[] strArrays2 = strArrays;
if (this.CurrentPage == num)
{
str1 = null;
}
else
{
str1 = num.ToString();
}
strArrays2[1] = str1;
string[] strArrays3 = strArrays;
if (this.CurrentPage == num1)
{
str2 = null;
}
else
{
str2 = num1.ToString();
}
strArrays3[2] = str2;
string[] strArrays4 = strArrays;
if (this.CurrentPage == this.PageCount)
{
str3 = null;
}
else
{
str3 = this.PageCount.ToString();
}
strArrays4[3] = str3;
strArrays[4] = pagerUrl;
return strArrays;
}
private string GetPagerUrl()
{
StringBuilder stringBuilder = new StringBuilder(HttpContext.Current.Request.Path);
stringBuilder.Append('?');
NameValueCollection queryString = HttpContext.Current.Request.QueryString;
for (int i = 0; i < queryString.Count; i++)
{
if (queryString.GetKey(i) != this.PageParamName)
{
stringBuilder.Append(queryString.GetKey(i));
stringBuilder.Append('=');
stringBuilder.Append(HttpContext.Current.Server.UrlEncode(queryString.Get(i)));
stringBuilder.Append('&');
}
}
stringBuilder.Append(this.PageParamName);
stringBuilder.Append('=');
return stringBuilder.ToString();
}
protected virtual void OnPageIndexChange(EventArgs e)
{
if (this.PageIndexChange != null)
{
this.PageIndexChange(this, e);
}
}
public void RaisePostBackEvent(string eventArgument)
{
this.CurrentPage = int.Parse(eventArgument);
this.OnPageIndexChange(new EventArgs());
}
protected override void Render(HtmlTextWriter writer)
{
this.GetHTML(writer);
base.Render(writer);
}
public event EventHandler PageIndexChange;
}
}
分页.CSS:
.pageclassstyle
{
display:block;float: left; cursor:pointer;
width: 60px;
height: 28px;
border: 1px solid #ddd;
text-align: center;
line-height: 30px;
border-left: none;
}
.pageclassstyle:hover{ color:#3399d5; background:#f5f5f5;}
.pageclassstylehome{ margin-left:35%;}
.wenjiancaozuo{ display:block; position:absolute; z-index:999; color:#056dae !important; padding:2px 8px; border-radius:2px; font-size:12px; margin-left: 152px;
margin-top: -18px;}