新客网WWW.XKER.COM:致力做中国最专业的网络学院!
模糊搜索
标题搜索
最新更新
新客商城
网站地图
RSS订阅
首页
新闻中心
网络学院
软件开发
站长之家
软件下载
手机学院
桌面壁纸
专题
论坛
学院:
操作系统
-
网络应用
-
服务器
-
网络安全
-
工具软件
-
办公软件
-
Web开发
-
数据库
-
网页设计
-
图形图像
-
媒体动画
-
硬件学堂
-
存储频道
-
QQ专区
您的位置:
首页
>
软件开发
>
.Net开发
>
Asp.net教程
> 正文:实现DataList控件的分页
实现DataList控件的分页
新客网
XKER.COM
2005-06-11 来源:
收藏本文
DataList控件功能很强大,他支持选择、编辑,实现的方法也很简单,不过最令人头疼的就是它不像DataGrid控件一样内置了分页的功能,这么好的一个控件竟然不能分页!!!确实是一个很让人头疼的事情。
不过,只是DataList没有提供内置的分页功能,但是并不表示,我们不能使用DataList控件来实现分页,既然它不给我分页功能,那只好自己动手了。
下面是全部原代码,其实用到的方法和PHP中的分页差不多,只是这里用的是DataAdapter与DataSet组合,而不是PHP中的SQL语句直接搞定。
default.aspx文件:
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="guestbook._default" %>
<HTML>
<HEAD>
<title>中国BS网留言簿</title>
<HTML>
<HEAD>
<title>DataList分页</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form method="post" action="default.aspx" runat="server" ID="Form1">
<table width="100%" border="0" cellpadding="2" cellspacing="0" align="center">
<tr>
<td align="middle">
<asp:DataList ID="Myguest" RepeatColumns="1" Runat="server">
<ItemTemplate>
<table class="gueststyleline" border="0" cellpadding="0" cellspacing="1" align="center" width="610">
<tr>
<td>
<table class="gueststyleline" border="0" cellpadding="0" cellspacing="1" align="center" width="610">
<tr>
<td>
<table width="100%" border="0" cellpadding="6" cellspacing="0" class="gueststylebg" align="center">
<tr>
<td width="112" align="center" height="153" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%" class="gueststylebg">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0" align="center" height="25">
<tr>
<td class="FILTER">
<div align="center"><font style="LEFT: 0px; WIDTH: 100%; WORD-WRAP: break-word; 130; 0pt"><%# DataBinder.Eval(Container.DataItem,"name") %></font></div>
</td>
</tr>
</table>
<div align="center"><font style="LEFT: 0px; WIDTH: 100%; WORD-WRAP: break-word; 130; 0pt"><img src="<%# DataBinder.Eval(Container.DataItem,"face") %>" width="80" height="90" align="middle"></font><br>
<br>
★
<%# DataBinder.Eval(Container.DataItem,"sex") %>
★</div>
</td>
</tr>
</table>
</td>
<td align="center" height="153" width="2">
<table class="gueststyleline" width="1" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="1"></td>
</tr>
</table>
</td>
<td height="33" valign="top" width="463">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="gueststylebg" style="TABLE-LAYOUT: fixed" height="20">
<tr>
<td height="14" width="310"><img src="images/icon/subject.gif" width="15" height="15" alt="主题">:<%# DataBinder.Eval(Container.DataItem,"caption") %><br>
</td>
<td width="151" height="14"><font style="0pt"> <img src="images/icon/posttime.gif" width="16" height="15" alt="发表时间[北京时间]">:<%# DataBinder.Eval(Container.DataItem,"postdate") %></font></td>
</tr>
</table>
<hr class="gueststylehr" size="0.1" width="100%">
<table width="100%" border="0" cellpadding="5" cellspacing="0" class="gueststylebg" height="45%" style="TABLE-LAYOUT: fixed">
<tr>
<td valign="top" width="28" align="center"><img src="images/emot/1.gif" width="16" height="16">
</td>
<td valign="top" width="94%" style="LEFT: 0px; WIDTH: 100%; WORD-WRAP: break-word; 130; 0pt">
<%# DataBinder.Eval(Container.DataItem,"content") %>
</td>
</tr>
</table>
<table width="100%" border="0" cellpadding="4" cellspacing="0">
<tr>
<td valign="top">
<hr class="gueststylehr" size="0.1" width="100%">
<a href="<%# DataBinder.Eval(Container.DataItem,"homepage") %>" target="_blank"><img src="images/icon/home.gif" width="44" height="16" border="0"></a>
<a href="http://www.chinagz.net/ip/ipsearch.cgi?ip=<%# DataBinder.Eval(Container.DataItem,"ip") %>" target="_blank"><img src="images/icon/ip.gif" align="absmiddle" height="16" border="0"></a>
<a href="http://search.tencent.com/cgi-bin/friend/user_show_info?ln=<%# DataBinder.Eval(Container.DataItem,"qq") %>" target="_blank">
<img src="images/icon/qq.gif" border="0" width="35" height="16"></a>
<a href="mailto:<%# DataBinder.Eval(Container.DataItem,"email") %>"><img src="images/icon/email.gif" width="42" height="16" border="0" alt=""></a>
<a href="javascript:;" onClick="MM_showHideLayers("admin","","hide","postcontent","","show")">
<img src="images/icon/postguest.gif" width="45" height="16" border="0"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<BR>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="2" align="center" height="9">
<TBODY>
<tr>
<td align="middle">
<table class="gueststyleline" width="610" border="0" cellpadding="1" cellspacing="0" height="18">
<tr>
<td height="24">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="gueststylebg" height="18">
<tr>
<td width="35%" align="middle" height="36">
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录 当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页
</td>
<td align="middle" height="36">
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" runat="server" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</TBODY>
</table>
</form>
</body>
</HTML>
注意:必须把两个LinkButton控件的Command事件改为Page_OnClick:
codebehind文件:
private void Page_Load(object sender, System.EventArgs e)
{
PageSize=1;
Conn= new SqlConnection(Application["Guest_Conn"].ToString());
Conn.Open();
if(!Page.IsPostBack)
{
ListBind();
CurrentPage = 0;
ViewState["PageIndex"] = 0;
//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
//计算总共有多少页
PageCount = RecordCount/PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from guest";
SqlCommand MyComm = new SqlCommand (strCount,Conn);
SqlDataReader dr = MyComm.ExecuteReader();
if(dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
ICollection CreateSource()
{
int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "Select * from guest order by postdate desc";
DataSet ds = new DataSet();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,Conn);
MyAdapter.Fill(ds,StartIndex,PageSize,"guest");
return ds.Tables["guest"].DefaultView;
}
public void ListBind()
{
Myguest.DataSource = CreateSource();
Myguest.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage+1).ToString();
}
public void Page_OnClick(Object sender,CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch(cmd)
{
case "next":
if(CurrentPage<(PageCount-1)) CurrentPage++;
break;
case "prev":
if(CurrentPage>0) CurrentPage--;
break;
}
ViewState["PageIndex"] = CurrentPage;
ListBind();
}
上一篇:
使用嵌套的Repeater控件
下一篇:
使用RangeValidator
【
收藏
】 【
评论
】 【
推荐
】 【
投稿
】 【
打印
】 【
关闭
】
最新相关文章
·
身份证函数 查看身份证地区信息
·
VS2008 第一次安装心得及使用
·
ASP.NET 2.0跨网页提交的三法
·
编程实例 WebGroupBox(Aspx控件)
·
asp.net mvc脚手架代码生成工具
·
用独立的DLL来存储图片(资源文件)
·
ASP.NET中多国语言的实现方法
·
实例 .net生成静态页方法总结
·
ASP.NET控件学习笔记之ViewState
·
用递归在TreeView价节点
·
经验总结 关于.NET 中的Event机制
·
.NET应用程序开发标准化(z)
·
在DataTable中查询应该注意的问题
·
LINQ 中调用存储过程自动绑定列名
·
如何用.NET技术在线生成网站LOGO
·
对于访问IIS元数据库失败的解决
发表评论
(
要记得去论坛讨论,点击注册新会员
)
匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
Asp.net Ajax 中的脚本错误: Sys未定义 的解决方
身份证号码15位升18位(C#)
asp.net ajax学习系列功能强大的UpdatePanel控件
Web Service描述语言 WSDL 详解(1)--为什么使用WS
Asp.Net Unleashed 2nd Edition 学习笔记 第三部
UpdatePanel与UrlRewrite
DataGridView 的分页处理
从资源文件里加载文件(C#)
Javascript与asp.net 实现Ajax多文件无刷新上传
关于ASP.NET调用JavaScript的实现
asp.net面试试题收集
基于ASP.NET AJAX的WebPart开发与部署
Huffman 编码简介(讲解的更好一些,有C的分析)
在VC++应用程序中读取文本数据
技巧 .NET如何访问MySQL数据库
专题教程
非主流Ps教程
磁碟机病毒专杀
AV终结者
会声会影教程
nero教程
FreeBSD使用大全
PDF阅读器
BT下载
QQ防骗术案例剖析
机器狗病毒查杀专题
搜狗拼音输入法专题
google earth专题
随机推荐
用XML写的留言录(vb.net)(五)
将ASP.net中的Table中的数据导入到Execl
VB.NET实现DirectDraw9 (1) 托管的DDraw
【C#】Decimal的类型判断!
几个很不错的.NET 相关的FAQ和例子代码的连接
解决了一直困惑我的接口问题
多线程应用程序中调用窗体的一点心得
.Net反射技术应用解决对象不同版本方法不同参数的
关于webcontrol和pagelet的一点看法
ADO.NET中的多数据表操作浅析—修改
自己用的一个求字符串长度的东东,大家可以编译了
八皇后问题的C#解答
保护 XML Web 服务免受黑客攻击, [第二部分]
在ASP.NET下实现数字和字符相混合的验证码
引言:Kent Beck 曾经说过“我只是个更注重的程序
实用信息推荐
常用网页广告代码全集
中国电子地图
IP查询、IP签名
免费代理IP(每日更新)
免费周公解梦大全
html互转JS
FLASH休闲小游戏
flash动画
qq搞笑表情
最新电影大片
戴尔dell笔记本电脑订购
戴尔dell台式机电脑订购