随手写个Repeater分页控件,奉上自己源码,欢迎大虾挑刺
每次用Repeater控件,总会牵扯到分页问题,索性自己重写一个Repeater控件,将分页功能集成进去,以后就不会这么麻烦了。废话少说贴上Code:
新建一个ASP.NET 服务器控件项目,添加一个Repeater类,继承System.Web.UI.WebControls.Repeater,然后开始在这个Repeater类里面写代码。
1、首先设置一下DefaultProperty的值
DefaultProperty:[DefaultProperty("Text"), ToolboxData("<!--Repeater Begin --><{0}:Repeater ID=\"rpList\" runat=server EnableViewState=\"false\" ><HeaderTemplate></HeaderTemplate><ItemTemplate></ItemTemplate><FooterTemplate></FooterTemplate></{0}:Repeater><!--Repeater End -->")],这里就写Repeater 最长用的几个东西,大家可以根据自己需要进行扩展。
2、添加几个分页要用的属性
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private int _pagesize = 0;
private string _pagelink = "";
private int _currentpage = 1;
[Bindable(true), Category("Data"), DefaultValue("1"), Description("记录总数")]
public int RecordCount
{
get
{
return _recordcount;
}
set
{
_recordcount = value;
}
}
[Bindable(true), Category("Data"), DefaultValue("1"), Description("每页显示记录数")]
public int PageSize
{
get
{
return _pagesize;
}
set
{
_pagesize = value;
}
}
[Bindable(true), Category("Data"), DefaultValue(""), Description("当前页链接")]
public string PageLink
{
get
{
return _pagelink;
}
set
{
_pagelink = value;
}
}
[Bindable(true), Category("Data"), DefaultValue("1"), Description("当前页")]
public int CurrentPage
{
get
{
return _currentpage;
}
set
{
_currentpage = value;
}
}
3、添加输出到HTML的方法
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
/// 输出html,在浏览器中显示控件
/// </summary>
/// <param name="output"> 要写出到的HTML</param>
protected override void Render(HtmlTextWriter output)
{
base.Render(output);
output.WriteLine("<div style=\"font-size:15px;color:Blue\">" + Pagination(_recordcount, _pagesize, _currentpage, _pagelink) + "</div>");
}
/// <summary>
/// 分页函数
/// </summary>
/// <param name="recordcount">总记录数</param>
/// <param name="pagesize">每页记录数</param>
/// <param name="currentpage">当前页数</param>
/// <param name="url">Url参数</param>
/// <returns></returns>
public string Pagination(int recordcount, int pagesize, int currentpage, string url)
{
int allcurrentpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string currentpagestr = "";
if (currentpage < 1)
{
currentpage = 1;
}
//计算总页数
if (pagesize != 0)
{
allcurrentpage = (recordcount / pagesize);
allcurrentpage = ((recordcount % pagesize) != 0 ? allcurrentpage + 1 : allcurrentpage);
allcurrentpage = (allcurrentpage == 0 ? 1 : allcurrentpage);
}
next = currentpage + 1;
pre = currentpage - 1;
startcount = (currentpage + 5) > allcurrentpage ? allcurrentpage - 9 : currentpage - 4;//中间页起始序号
//中间页终止序号
endcount = currentpage < 5 ? 10 : currentpage + 5;
//为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (startcount < 1) { startcount = 1; }
//页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
if (allcurrentpage < endcount) { endcount = allcurrentpage; }
currentpagestr = "总页数:" + allcurrentpage + " 当前页:" + currentpage + " 每页" + pagesize + "条记录 ";
currentpagestr += currentpage > 1 ? "<a href=\"" + url + "?page=1\">首页</a> <a href=\"" + url + "?page=" + pre + "\">上一页</a>" : "首页 上一页";
//中间页处理
for (int i = startcount; i <= endcount; i++)
{
currentpagestr += currentpage == i ? " <font color=\"#ff0000\">" + i + "</font>" : " <a href=\"" + url + "?page=" + i + "\">" + i + "</a>";
}
currentpagestr += currentpage != allcurrentpage ? " <a href=\"" + url + "?page=" + next + "\">下一页</a> <a href=\"" + url + "?page=" + allcurrentpage + "\">末页</a>" : " 下一页 末页";
return currentpagestr;
}
4、在项目中添加这个控件的引用
/Files/yangtongnet/SRepeater.rar
5、在页面中添加命名空间
<%@ Register Assembly="ServerControl" Namespace="ServerControl" TagPrefix="SControl" %>
6、控件的客户端代码
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<!--Repeater Begin -->
<SControl:Repeater ID="rpList" runat="server" EnableViewState="false">
<HeaderTemplate>
<table class="tby">
<tr>
<th>ID</th>
<th>IPFrom</th>
<th>IPTo</th>
<th>IPLocation</th>
<th>City</th>
<th>IPToNumber</th>
<th>IPFromNumber</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("IPid")%></td>
<td><%#Eval("IPFrom")%></td>
<td><%#Eval("IPTo")%></td>
<td><%#Eval("IPLocation")%></td>
<td><%#Eval("IPCity") %></td>
<td><%#Eval("IPToNumber")%></td>
<td><%#Eval("IPFromNumber")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</SControl:Repeater>
<!--Repeater End -->
7、后台代码
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
{
this.rpList.PageSize = 10;
this.rpList.RecordCount = IPInfo_BLL.getDatabyCount();
this.rpList.PageLink = ConfigurationManager.AppSettings["PageUrl1"].ToString();
this.rpList.CurrentPage = Request.QueryString["page"] == null ? 1 : int.Parse(Request.QueryString["page"]);
int SIndex = (this.rpList.CurrentPage-1) * 10 + 1;
this.rpList.DataSource = IPInfo_BLL.getDatabyIndex(SIndex, this.rpList.PageSize);
this.rpList.DataBind();
}
//分页
public static IQueryable getDatabyIndex(int startIndex,int PageSize)
{
var query = from p in datacontext.IPInfo
select new
{
p.IPid,
p.IPFrom,
p.IPTo,
p.IPLocation,
p.IPCity,
p.IPToNumber,
p.IPFromNumber
};
return query.Skip(startIndex).Take(PageSize);
}
8、看看最后效果
当然了,最后样式不满意,可以自己修改吗,我这里只是简单的写下,以后根据需要扩展。
出处:http://www.cnblogs.com/yangtongnet/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。