C#.NET我是个新手。前几天跟同事在做项目时,用了Repeater控件来实现,因为以前都是用AspNetPager。不过这样就带来一个新问题:怎么分页?Repeater没有内置的分页功能。想写个自己的分页控件,名声好听些,后来后来在网上找了一些分页功能的资料,然后就自己写了一个,功能都按我的想法做出来了。本人从事美工做.net还没有1年时间,因为主要是做美工哪位高手有何高见请及时指正,很愿意与各位交流!(欢迎拍砖)(下载地址在文章最下面!)
效果如图:
tools/pages.ascx控件前台代码
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="pages.ascx.cs" Inherits="tools_page" %>
<div class="vc clearfix">
<p>当前第 <asp:Label ID="crpage" runat="server" Text="Label"></asp:Label> 页 / 共 <asp:Label ID="pgcount" runat="server" Text=""></asp:Label>
页 总共<asp:Label ID="jlcount" runat="server" Text=""></asp:Label>条记录 </p><asp:HyperLink ID="HLfst" runat="server" CssClass="fst"></asp:HyperLink><asp:HyperLink ID="HLpre" runat="server" CssClass="pre"></asp:HyperLink>
<%= pagehtml %><asp:HyperLink ID="HLnext" runat="server" CssClass="next"></asp:HyperLink><asp:HyperLink ID="HLlst" runat="server" CssClass="lst"></asp:HyperLink>
</div>
tools/pages.ascx.cs控件后台代码
using System;
using System.Data;
using System.Web.UI.WebControls;
public partial class tools_page : System.Web.UI.UserControl
{
public string pagehtml = "";
public int pagenum = 5;//设置每个分组有多少页码
int startindex = 1;//起始页码
protected void Page_Load(object sender, EventArgs e)
{}
public void showthanklist(DataTable dt, Repeater Repeater1, int curpage, string cururl,string abso,int pagesize)
{
//分页核心代码
PagedDataSource pagedata = new PagedDataSource();
//DataTable dt = LoadRepeater(id.ToString());
pagedata.DataSource = dt.DefaultView;
pagedata.PageSize = pagesize;
pagedata.AllowPaging = true;
int half = 1;
while ((half + 1) * 2 < pagenum) { half++; }
int start = 1;
int end = 1;
if (curpage > pagedata.PageCount || curpage < 1) curpage = 1;
pagedata.CurrentPageIndex = curpage - 1;
//如果页面总数小于分组数量.
if (pagenum >= pagedata.PageCount)
{ end = pagedata.PageCount; getPageindex(start, end, curpage, cururl, pagesize); }
else
{
if (curpage - half > 0)
{
start = curpage - half;
if (curpage + half > pagedata.PageCount)
{
start = pagedata.PageCount - pagenum + 1;
end = pagedata.PageCount;
}
else
end = curpage + half;
}
else
{
start = 1;
end = pagenum > pagedata.PageCount ? pagedata.PageCount : pagenum;
}
getPageindex(start, end, curpage, cururl, pagesize);
}
jlcount.Text = Convert.ToString(dt.Rows.Count);
crpage.Text = Convert.ToString(curpage);
pgcount.Text = Convert.ToString(pagedata.PageCount);
if (pagedata.PageCount == 1) //如果只有一个分组
{
HLpre.CssClass = "prefalse";
HLnext.CssClass = "nextfalse";
HLfst.CssClass = "fstfalse";
HLlst.CssClass = "lstfalse";
}
else if (curpage == pagedata.PageCount && curpage > 1)//如果当前分组是最后一个分组
{
HLpre.Enabled = true;
HLnext.Enabled = false;
HLpre.NavigateUrl = abso + cururl + "page=" + (curpage - 1);
HLfst.Enabled = true;
HLfst.NavigateUrl = abso + cururl + "page=1";
HLfst.Enabled = true;
HLfst.NavigateUrl = abso+ cururl + "page=1";
HLnext.CssClass = "nextfalse";
HLlst.CssClass = "lstfalse";
}
else if (curpage == 1 && curpage < pagedata.PageCount)
{
HLpre.Enabled = false;
HLnext.Enabled = true;
HLfst.Enabled = false;
HLlst.Enabled = true;
HLlst.NavigateUrl = abso + cururl + "page=" + Convert.ToString(pagedata.PageCount);
HLnext.NavigateUrl = abso + cururl + "page=" + (curpage + 1);
HLfst.CssClass = "fstfalse";
HLpre.CssClass = "prefalse";
}
if (curpage > 1 && curpage < pagedata.PageCount)//如果当前分组处在中间
{
HLpre.Enabled = true;
HLpre.NavigateUrl = abso + cururl + "page=" + (curpage - 1);
HLnext.NavigateUrl = abso + cururl + "page=" + (curpage + 1);
HLnext.Enabled = true;
HLfst.Enabled = true;
HLfst.NavigateUrl = abso + cururl + "page=1";
HLlst.Enabled = true;
HLlst.NavigateUrl = abso + cururl + "page=" + Convert.ToString(pagedata.PageCount);
}
Repeater1.DataSource = pagedata;
Repeater1.DataBind();
}
//设置分页样式
private void getPageindex(int start, int end, int curpage,string cururl,int pagesize)
{
pagehtml = "";
for (int i = start; i <= end - 1; i++)
{
if (curpage == i) { pagehtml += "<b href=‘" + cururl + "page=" + Convert.ToString(i + startindex - 1) + "‘ class=‘aa" + Convert.ToString(i + startindex - 1) + "‘>" + Convert.ToString(i + startindex - 1) + "</b>"; }
else
pagehtml += "<a href=‘" + cururl + "page=" + Convert.ToString(i + startindex - 1) + "‘ class=‘a" + Convert.ToString(i + startindex - 1) + "‘>" + Convert.ToString(i + startindex - 1) + "</a>";
}
if (curpage == end) { pagehtml += "<b id=‘lastid‘ href=‘" + cururl + "page=" + Convert.ToString(end + startindex - 1) + "‘ class=‘aa" + Convert.ToString(end + startindex - 1) + "‘>" + Convert.ToString(end + startindex - 1) + "</b>"; }
else
{
pagehtml += "<a id=‘lastid‘ href=‘" + cururl + "page=" + Convert.ToString(end + startindex - 1) + "‘ class=‘a" + Convert.ToString(end + startindex - 1) + "‘>" + Convert.ToString(end + startindex - 1) + "</a>";
}
}
}
DEMO.aspx前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DEMO.aspx.cs" Inherits="DEMO" %>
<%@ Register Src="tools/pages.ascx" TagName="pages" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>我的分页控件-幸凡在线学习网</title>
<link href="css/index.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div class="com">
<ul>
<asp:Repeater ID="art_list" runat="server">
<ItemTemplate>
<li>
<a href="http://www.86y.org/art_detail.aspx?id=<%# Eval("id") %>" title="<%# Eval("title") %>"><%# Eval("title") %></a>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
<uc1:pages ID="pages1" runat="server" />
</div>
</form>
</body>
</html>
DEMO.aspx.cs后台代码
using System;
using System.Data;
using System.Data.OleDb;
public partial class DEMO : System.Web.UI.Page
{
string cururl = "DEMO.aspx?";//当前页面链接
public int curpage = 0;//初始页
public int pagesize = 10; //设置每页显示多少条记录
protected void Page_Load(object sender, EventArgs e)
{
//获取当前页码
if (!this.IsPostBack)
{
if (Request.Params["page"] == null)
curpage = 1;
else
{
string temp = Request.Params["page"].ToString();
curpage = Convert.ToInt32(temp);
}
}
DataTable dt = art_table();
//showthanklist(DataTable dt, Repeater Repeater1, int curpage, string cururl,string abso,int pagesize)使用方法
//dt:是DatatTable,art_list:前台显示Repeater的ID,curpage:当前页,cururl:当前页面的链接,abso:相对于tools里面的使用路径因为是在根目录调用所以需要加../,pagesize:每页显示多少条
pages1.showthanklist(dt, art_list, curpage, cururl, "../", pagesize);
}
//查出所有文章数据
public DataTable art_table()
{
string sql="";
sql = "select * from art order by id desc";
DataTable btab = selects(sql);
return btab;
}
//创建数据库链接
public OleDbConnection GetConnection()
{
try
{
OleDbConnection myconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\#ex.mdb;Jet OLEDB:Database Password=");
myconn.Open();
return myconn;
}
catch (System.Data.OleDb.OleDbException e)
{
string aa = e.Message;
return null;
}
}
//查询记录
public DataTable selects(string sql)
{
OleDbCommand sc = new OleDbCommand(sql, GetConnection());
OleDbDataAdapter ad = new OleDbDataAdapter(sc);
DataSet ds = new DataSet();
ad.Fill(ds);
ad.Dispose();
if (sc.Connection.State != ConnectionState.Closed)
sc.Connection.Close();
sc.Dispose();
return ds.Tables[0];
}
}
源码下载地址链接: http://pan.baidu.com/s/1pKAAHFd 密码: jwvf