private string GetContents()
{
this._TotalPage = ((this.TotalRecord / this.Info.PageSize) * this.Info.PageSize == this.TotalRecord) ? (this.TotalRecord / this.Info.PageSize) : ((this.TotalRecord / this.Info.PageSize) + 1);
int BeginRecord = (this.PageIndex - 1) * this.Info.PageSize + 1;
int EndRecord = Math.Min(this.PageIndex * this.Info.PageSize, this.TotalRecord);
string PageInfo = string.Format("[每页<span>{0}({1}-{2})</span>条 共<span>{3}</span>条<span>{4}</span>页]", Info.PageSize, BeginRecord, EndRecord, this.TotalRecord, this.TotalPage);
StringBuilder PageListStr = new StringBuilder();
string PageIndexColor = "#000000";
int SingleNumber = this.TotalPage - (TotalPage / BarSize) * BarSize;
int IntPageForMax = (this.PageIndex - 1) / BarSize;
int MinInt = (1 + BarSize * IntPageForMax);
int MaxInt = ((IntPageForMax + 1) * BarSize) > TotalPage ? TotalPage : ((IntPageForMax + 1) * BarSize);
if (this.TotalRecord == 0 || this.TotalPage == 0)
{
PageListStr.AppendFormat("<span{0};margin:auto 3px;'>0</span>", PageIndexColor);
PageListStr.AppendFormat(" [共<span>0</span>页/当前第<span>0</span>页 共<span>0</span>条记录,当前记录数<span>0</span>到<span>0</span>]");
return PageListStr.ToString();
}
else
{
if (this.TotalPage <= this.BarSize)
{
for (int i = 1; i <= TotalPage; i++)
{
PageIndexColor = PageIndex == i ? "#CC0000" : "#000000";
if (PageIndex == i)
PageListStr.AppendFormat("<a>{2}</a>", this.UniqueID, PageIndexColor, i);
else
PageListStr.AppendFormat("<a href=https://www.jb51.net/article/\"javascript:{2}\">{3}</a>", this.UniqueID, PageIndexColor, Page.ClientScript.GetCallbackEventReference(this, i.ToString(),"AjaxPagerCallBack",null), i);
}
PageListStr.AppendFormat(" {0}", PageInfo);
return PageListStr.ToString();
}
else
{
for (int i = MinInt; i <= MaxInt; i++)
{
PageIndexColor = PageIndex == i ? "#CC0000" : "#000000";
if (PageIndex == i)
PageListStr.AppendFormat("<a id={0}'>{2}</a>", this.UniqueID, PageIndexColor, i);
else
PageListStr.AppendFormat("<a href=https://www.jb51.net/article/\"javascript:{2}\">{3}</a>", this.UniqueID, PageIndexColor, Page.ClientScript.GetCallbackEventReference(this, i.ToString(), "AjaxPagerCallBack", null), i);
}
if (PageIndex <= BarSize && TotalPage > BarSize)
{
PageListStr.AppendFormat("<a href=https://www.jb51.net/article/\"javascript:{1}\">下一页</a>", this.UniqueID, Page.ClientScript.GetCallbackEventReference(this, System.Convert.ToString(BarSize + 1), "AjaxPagerCallBack", null));
}
if (this.PageIndex > BarSize && (TotalPage - this.PageIndex) >= SingleNumber)
{
int MultiMinPageIndex = (IntPageForMax * BarSize);
int MultiMaxPageIndex = ((IntPageForMax + 1) * BarSize) + 1;
PageListStr.Insert(0, string.Format("<a href=https://www.jb51.net/article/\"javascript:{1}\">上一页</a>", this.UniqueID, Page.ClientScript.GetCallbackEventReference(this, MultiMinPageIndex.ToString(),"AjaxPagerCallBack",null)));
PageListStr.AppendFormat("<a href=https://www.jb51.net/article/\"javascript:{1}\">下一页</a>", this.UniqueID, Page.ClientScript.GetCallbackEventReference(this, MultiMaxPageIndex.ToString(),"AjaxPagerCallBack",null));
}
if (PageIndex > 10 && (TotalPage - PageIndex) < SingleNumber)
{
int MultiMinPageIndex = (IntPageForMax * BarSize);
PageListStr.Insert(0, string.Format("<a href=https://www.jb51.net/article/\"javascript:{1}\">上一页</a>", this.UniqueID, Page.ClientScript.GetCallbackEventReference(this, MultiMinPageIndex.ToString(), "AjaxPagerCallBack", null)));
}
PageListStr.AppendFormat(" {0}", PageInfo);
return PageListStr.ToString();
}
}
}
public void BindData()
{
Repeater rpt = getRpt();
rpt.Visible = true;
SqlHelper helper;
helper = this.Info.ConnectStringName.IsNullOrEmpty() ? new SqlHelper() : new SqlHelper(Info.ConnectStringName);
if (this.Info.RepeaterUniqueID.IsNullOrEmpty())
{
throw new Exception("必须给Info的RepeaterUniqueID属性赋值");
}
int count = 0;
DataTable dt = helper.GetPageData(Info.TableName, Info.Fields, Info.IdentityField, Info.PageSize, PageIndex, Info.IsDesc, Info.Content, out count);
this.TotalRecord = count;
rpt.DataShow(dt);
}
private Repeater getRpt()
{
return this.Page.FindControl(this.Info.RepeaterUniqueID) as Repeater;
}
先感谢一下写那个Pager的人,GetContents(得到自己分页后的HTML)里我只做了少许改动,要不然还得细细算来!!
BindData(用到了我的SqlHelper)是利用为服务器的DataBind()方法把数据放到repeater里,只是不让它呈示,嘿嘿!
getRpt只是找到Repeater引用
维护视图状态
复制代码 代码如下: