前台
protected void Page_Load(object sender, EventArgs e)
{
Bound();
}
这个是点击不同页触发的事件,一样要哦
protected void XRepeater1_Click(object sender, EventArgs e)
{
Bound();
}
private void Bound()
{
DataTable ds = SqlHelper.ExecuteDataTable("SELECT * FROM Table1",CommandType.Text,null);
XRepeater1.DataSource = ds;
XRepeater1.DataBind();
}
类处理文件
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
namespace WEBCONTRAL
{
public class XRepeater : Repeater, INamingContainer, IPostBackEventHandler, IPostBackDataHandler
{
private int _pageSize=10;
private DefinePageStyle _pageStyle;
private bool _allowPage = true;
private bool _definePageCount;
private IList _dataSocure;
private DataTable _dataTable;
// 声明Click事件委托
public event EventHandler Click;
public enum DefinePageStyle
{
Normal=0,
Number =1
}
[Browsable(true),
Description("是否设置记录总数"),
Category("Page"),
DefaultValue(true)]
public bool DefinePageCount
{
get
{
return _definePageCount;
}
set
{
_definePageCount = value;
}
}
[Browsable(true),
Description("分页样式:/nNormal为普通,Number为数字"),
Category("Page"),
DefaultValue(0)]
public DefinePageStyle PageStyle
{
get
{
return _pageStyle;
}
set
{
_pageStyle = value;
}
}
[Browsable(true),
Description("是否分页"),
Category("Page"),
DefaultValue(true)]
public bool AllowPage
{
get
{
return _allowPage;
}
set
{
_allowPage = value;
}
}
[Browsable(true),
Description("当前页"),
Category("Page"),
DefaultValue(1)]
public int PageNo
{
get
{
return ViewState["PageNo"] == null ? 1 : (int)ViewState["PageNo"];
}
set
{
ViewState["PageNo"] = value;
}
}
[Browsable(true),
Description("分页数"),
Category("Page"),
DefaultValue(10)]
public int PageSize
{
get
{
return _pageSize;
}
set
{
_pageSize = value;
}
}
[Browsable(true),
Description("记录总数"),
Category("Page"),
DefaultValue(0)]
public int RecordCount
{
get
{
return ViewState["RecordCount"] == null ? 0 : (int)ViewState["RecordCount"];
}
set
{
ViewState["RecordCount"] = value;
}
}
private int PageCount
{
get
{
return Convert.ToInt32(Math.Ceiling((double)RecordCount / (double)_pageSize));
}
}
public override object DataSource
{
set
{
try
{
if (value is IList)
{
_dataSocure = (IList)value;
if (!DefinePageCount)
{
RecordCount = _dataSocure.Count;
}
}
else if (value is DataTable)
{
_dataTable = (DataTable)value;
if (!DefinePageCount)
{
RecordCount = _dataTable.Rows.Count;
}
}
}
catch
{
_dataSocure = null;
RecordCount = 0;
}
}
}
protected override void OnDataBinding(EventArgs e)
{
int start = _pageSize * (PageNo - 1);
int size = Math.Min(_pageSize, RecordCount - start);
if (_dataSocure != null)
{
if (DefinePageCount)
{
base.DataSource = _dataSocure;
}
else
{
IList page = new ArrayList();
for (int i = 0; i < size; i++)
page.Add(_dataSocure[start + i]);
base.DataSource = page;
}
}
else if(_dataTable!=null)
{
if (DefinePageCount)
{
base.DataSource = _dataTable;
}
else
{
DataTable dt = new DataTable();
dt = _dataTable.Clone();
for (int i = 0; i < size; i++)
dt.ImportRow(_dataTable.Rows[start + i]);
base.DataSource = dt;
}
}
base.OnDataBinding(e);
}
private void PageTypeDefault(ref StringBuilder content)
{
if (PageNo > 1)
{
content.Append("<a href=/"javascript:" + Page.ClientScript.GetPostBackEventReference(this, "top") + "/">首页</a>");
content.Append(" ");
content.Append("<a href=/"javascript:" + Page.ClientScript.GetPostBackEventReference(this, "pre") + "/">上一页</a>");
content.Append(" ");
}
else
{
content.Append("首页");
content.Append(" ");
content.Append("上一页");
content.Append(" ");
}
if (PageNo < PageCount)
{
content.Append("<a href=/"javascript:" + Page.ClientScript.GetPostBackEventReference(this, "next") + "/">下一页</a>");
content.Append(" ");
content.Append("<a href=/"javascript:" + Page.ClientScript.GetPostBackEventReference(this, "last") + "/">尾页</a>");
content.Append(" ");
}
else
{
content.Append("下一页");
content.Append(" ");
content.Append("尾页");
content.Append(" ");
}
}
private void PageTypeNumber(ref StringBuilder content)
{
int start, end;
start = PageNo - 4;
end = PageNo + 4;
content.Append("<a href=/"javascript:" + Page.ClientScript.GetPostBackEventReference(this, "1") + "/">1</a> ");
if (PageNo > 6)
{
content.Append("...");
}
else
{
start = 2;
end = start + 7;
}
if (PageCount - PageNo <= 5)
{
end = PageCount - 1;
start = end - 7;
}
if (start < 1)
start = 2;
if (end > PageCount - 1)
end = PageCount - 1;
for (int i = start; i <= end; i++)
{
content.Append("<a href=/"javascript:" + Page.ClientScript.GetPostBackEventReference(this, i.ToString()) + "/">"+i.ToString()+"</a> ");
}
if (PageCount - PageNo > 5)
{
content.Append("...");
}
content.Append("<a href=/"javascript:" + Page.ClientScript.GetPostBackEventReference(this, PageCount.ToString()) + "/">" + PageCount.ToString() + "</a>");
}
protected override void OnPreRender(EventArgs e)
{
StringBuilder css = new StringBuilder();
css.Append("<style type=/"text/css/">/n");
css.Append("ul{ list-style:none; margin:0; padding:0;}/n");
css.Append("a:link,a:visited {font-size:16px;text-decoration: none; color:#555;}/n");
css.Append("a:hover{font-size:16px;text-decoration:underline; color:#000;}/n");
css.Append("</style>");
System.Web.HttpContext.Current.Response.Write(css.ToString());
base.OnPreRender(e);
}
protected override void Render(HtmlTextWriter output)
{
base.Render(output);
if (_allowPage)
{
StringBuilder content = new StringBuilder(1000);
content.Append("<table width=/"80%/"><tr><td>");
if (PageStyle == DefinePageStyle.Number)
{
PageTypeNumber(ref content);
}
else if (PageStyle == DefinePageStyle.Normal)
{
PageTypeDefault(ref content);
}
output.Write(content.ToString());
output.Write(" ");
if (PageStyle == DefinePageStyle.Number)
{
output.Write("<input name='" + this.UniqueID + "_input' type='text' style='width:25px;' Onblur='CheckMyPageNo(this)' value='" + PageNo.ToString() + "'>");
output.Write(" ");
output.Write("<input id='" + this.UniqueID + "_onclick' type=submit value='' style='display:none;' name='" + this.UniqueID + "'>");
output.Write("<a href='javascript:document.getElementById(/"" + this.UniqueID + "_onclick/").click();'>Go</a>");
}
else if (PageStyle == DefinePageStyle.Normal)
{
output.Write("<select name='" + this.UniqueID + "_page' οnchange=/"__doPostBack('" + this.UniqueID + "', this.value)/">");
string listoption = "";
for (int i = 1; i <= PageCount; i++)
{
listoption += "<option value=/"" + i.ToString() + "/"";
if (i == PageNo)
listoption += " selected ";
listoption += " >第" + i.ToString() + "页</option>";
}
output.Write(listoption);
output.Write("</select>");
}
output.Write(" ");
output.Write("共" + RecordCount + "条 ");
output.Write("页码:" + PageNo.ToString() + "/" + PageCount.ToString());
output.Write("</td></tr></table>");
output.Write("<script language=/"javascript/">/nfunction CheckMyPageNo(obj)/n{");
output.Write(" var re = /^//d+$/;/n");
output.Write("if (!re.test(obj.value)){alert(/"请输入正确数字/");obj.focus();obj.value=1;return}");
output.Write("var PageCount = " + PageCount + ";/n");
output.Write("if(obj.value>PageCount){obj.value=PageCount};/n");
output.Write("else if(obj.value<1){obj.value=1};/n");
output.Write("}</script>/n");
#region 注释
//output.AddAttribute(HtmlTextWriterAttribute.Type, "Submit");
//output.AddAttribute(HtmlTextWriterAttribute.Value, "GO");
//output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
//output.RenderBeginTag(HtmlTextWriterTag.Input);
//output.RenderEndTag();
//DropDownList list = new DropDownList();
//for (int i = 1; i <= PageCount; i++)
//{
// list.Items.Add(new ListItem("第"+i.ToString()+"页",i.ToString()));
//}
//list.Attributes.Add("onchange", "__doPostBack('" + this.UniqueID + "', this.value)");
//list.RenderControl(output);
//output.RenderBeginTag(HtmlTextWriterTag.Select);
//for (int i = 1; i <= PageCount; i++)
//{
// output.AddAttribute(HtmlTextWriterAttribute.Value, i.ToString());
// output.RenderBeginTag(HtmlTextWriterTag.Option);
// output.Write("第" + i.ToString() + "页");
// output.RenderEndTag();
//}
//output.RenderEndTag();
//output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
//output.AddStyleAttribute(HtmlTextWriterStyle.Width, "30px");
//output.AddAttribute("Onblur", "CheckMyPageNo(this)");
//output.AddAttribute(HtmlTextWriterAttribute.Value, PageNo.ToString());
//output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID + "_input");
//output.RenderBeginTag(HtmlTextWriterTag.Input);
//output.RenderEndTag();
#endregion
}
}
// 定义OnClick事件处理程序
protected virtual void OnClick(EventArgs e)
{
if (Click != null)
{
Click(this, e);
}
}
public void RaisePostBackEvent(string e)
{
//当发生回送的时候改变控件当前索引
if (e == "top")
{
PageNo = 1;
}
else if (e == "pre")
{
PageNo--;
}
else if (e == "next")
{
PageNo++;
}
else if (e == "last")
{
PageNo = PageCount;
}
else if (e!=null)
{
PageNo = Convert.ToInt32(e);
}
OnClick(EventArgs.Empty);
}
///<summary>
///当由类实现时,为 ASP.NET 服务器控件处理回发数据。
///</summary>
///<param name="postDataKey">数据集合元素索引</param>
///<param name="values">string 的排序集合</param>
///<returns></returns>
public bool LoadPostData(string pkey, NameValueCollection pcol)
{
PageNo = Convert.ToInt32(pcol[this.UniqueID + "_input"]);
Page.RegisterRequiresRaiseEvent(this);
return false;
}
///<summary>
///当由类实现时,用信号要求服务器控件对象通知 ASP.NET 应用程序该控件的状态已更改。
///</summary>
public void RaisePostDataChangedEvent()
{
}
}
}