主题:  Asp.Net分页程序演示

cmoonc

职务:普通成员
等级:1
金币:0.0
发贴:48
注册:2004/4/24 18:07:05
#12005/3/28 19:36:12
小弟也是.NET的初学者,总是爱在这里提问题,今天花了大半天时间总算是做了个分页的东东出来,现在贴出来,一方面供其他初学者学习,一方面提高手多多指点.

思路:利用DataAdapter的Fill方法实现分页(以下是完整的页面代码,样式很粗糙,只为研究程序而做):

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">

OleDbConnection conn; //声明所需变量
string conns,comms;
int PageSize,PageCount,CurrentPage,StartIndex,RecordCount;

protected void Page_Load(Object Src, EventArgs E)
{
conns="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("/Data/TEST.mdb"); //建立数据库连接
comms="Select * From Images";
conn=new OleDbConnection(conns);

PageSize=8; //此处设定每页所显示的记录数
RecordCount=CountRecord();//获取总的记录数
if((RecordCount%PageSize)==0)PageCount=RecordCount/PageSize; //获取总页面数
else PageCount=(RecordCount/PageSize)+1;

if(!Page.IsPostBack) //第一次访问时的设置
{
CurrentPage=1; //当前页为1
ViewState["CurrentPage"]=CurrentPage; //建立ViewState值以便传递
StartIndex=0; //设定读取起始行
BindData(StartIndex);
prev.Visible=false;
l2.Text="1";        
}
l1.Text=PageCount.ToString();
l3.Text=RecordCount.ToString();
}

int CountRecord() //计算记录数
{
DataSet DSc=new DataSet();
OleDbDataAdapter DAc=new OleDbDataAdapter(comms,conn);
DAc.Fill(DSc,"Images");
return DSc.Tables["Images"].Rows.Count;
}

void BindData(int StartIndex) //数据绑定
{
DataSet DS=new DataSet();
OleDbDataAdapter DA=new OleDbDataAdapter(comms,conn);
DA.Fill(DS,StartIndex,PageSize,"Images");
DataList1.DataSource=DS.Tables["Images"];
DataList1.DataBind();
}

void page_click(Object sender,CommandEventArgs e) //Click事件处理
{
string s=e.CommandName;
CurrentPage=(int)ViewState["CurrentPage"]; //取得传递的值

switch (s) //判断所点击的按钮
{
case "prev":
     CurrentPage-=1;
     break;
case "next":
     CurrentPage+=1;
     break;
case "last":
     CurrentPage=PageCount;
     break;
}

if(CurrentPage!=1)prev.Visible=true; //确定按钮的状态
else prev.Visible=false;
if(CurrentPage!=PageCount)next.Visible=true;
else next.Visible=false;

StartIndex=(CurrentPage-1)*PageSize; //设定读取起始行
BindData(StartIndex);
ViewState["CurrentPage"]=CurrentPage;
l2.Text=CurrentPage.ToString();
}
</script>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>分页演示</title>
</head>
<body>

<Form runat="server">
<asp:DataList ID="DataList1" RepeatColumns="4" RepeatDirection="Horizontal" runat="server">
<Itemtemplate>
Title:<asp:Label ID="Label1" runat="server" Text=<%# DataBinder.Eval(Container.DataItem,"title") %> />
</Itemtemplate>
</asp:DataList>
<hr>
<asp:HyperLink ID="H1" runat="server" Text="First" NavigateUrl="page.aspx"/>&nbsp;&nbsp;
<asp:LinkButton ID="prev" runat="server" CommandName="prev" Text="Prev" OnCommand="page_click"/>&nbsp;&nbsp;
<asp:LinkButton ID="next" runat="server" CommandName="next" Text="Next" OnCommand="page_click"/>&nbsp;&nbsp;
<asp:LinkButton ID="last" runat="server" CommandName="last" Text="Last" OnCommand="page_click"/>
</Form>

共有:<asp:Label ID="l1" runat="server"/>页&nbsp;&nbsp;
当前为:<asp:Label ID="l2" runat="server"/>页&nbsp;&nbsp;
共有:<asp:Label ID="l3" runat="server"/>条数据&nbsp;&nbsp;
</body>
</html>

编辑历史:[此帖最近一次被 allinhands 编辑过(编辑时间:2005-11-09 12:16:49)]


zm8111

职务:普通成员
等级:1
金币:0.0
发贴:3
注册:2005/4/3 14:03:22
#22005/4/3 14:17:58
大哥,把表情去掉啊,晕了,我学习学习阿



盗版德

职务:普通成员
等级:1
金币:0.0
发贴:2
注册:2005/10/10 9:20:23
#32005/11/9 12:01:17
就是!~

我可是一只笨鸟!`~
为何你的表情我复制过来就没有了呢???