新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:一个.net分页测试源码

一个.net分页测试源码

新客网 XKER.COM 2005-08-23 来源: 收藏本文
 
一个.net分页测试源码

作者:淘特网

出处:淘特网

注:转载请注明出处

 <% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.SqlClient" %>
<Script Language="C#" Runat="Server">
SqlConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage;
public void Page_Load(Object src,EventArgs e)
{
 //设定PageSize
 PageSize = 5;
 
 //连接语句
 string MyConnString = "server=127.0.0.1;database=example;uid=sa;pwd=1234567";
 MyConn = new SqlConnection(MyConnString);
 MyConn.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 TABLE1";
 SqlCommand MyComm = new SqlCommand(strCount,MyConn);
 SqlDataReader dr = MyComm.ExecuteReader();
 if(dr.Read())
 {
  //intCount = Int32.Parse(dr["co"].ToString());
  intCount=(int)dr["co"];
 }
 else
 {
  intCount = 0;
 }
 dr.Close();
 return intCount;
}

ICollection CreateSource()
{
 
 int StartIndex;
 
 //设定导入的起终地址
 StartIndex = CurrentPage*PageSize;
 string strSel = "select * from TABLE1";
 DataSet ds = new DataSet();

 SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,MyConn);
 MyAdapter.Fill(ds,StartIndex,PageSize,"Score");
 
 return ds.Tables["Score"].DefaultView;
}
public void ListBind()
{
 score.DataSource = CreateSource();
 score.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();
 
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server">
共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录&nbsp; 
当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页&nbsp;

<asp:DataList id="score" runat="server"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="Gainsboro"
EditItemStyle-BackColor="yellow"
>
 <ItemTemplate>
  姓名:<%# DataBinder.Eval(Container.DataItem,"title") %>
  <asp:LinkButton id="btnSelect" Text="编辑" CommandName="edit" runat="server" />
 </ItemTemplate>
</asp:DataList>
共<%=PageCount%>页
<asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
<asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />

</form>
</body>
</html>


收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐