首页 > 编程 > .NET > 正文

.net分页源码示例

2024-07-10 13:04:59
字体:
来源:转载
供稿:网友

<% @ 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>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表