- charrysong
- 等级: 初级会员
- 性别:
- 文章: 5
- 积分: 50
- 来自: 广州
|
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();
-
-
-
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.ok22.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();
-
-
-
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];
-
}
-
}
源码下载地址:自己写的repeater 分页控件(附源码下载)
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|