国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

一個.net分頁測試源碼

2024-07-10 12:58:57
字體:
來源:轉載
供稿:網友
  一個.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>


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武邑县| 平罗县| 新乐市| 应城市| 安西县| 天峨县| 巴林左旗| 册亨县| 施秉县| 银川市| 禄劝| 临沭县| 宜兴市| 乡宁县| 错那县| 自治县| 永和县| 大足县| 永嘉县| 张家口市| 翁源县| 乐平市| 衡阳县| 克拉玛依市| 慈溪市| 象州县| 桂平市| 台南县| 镇赉县| 马鞍山市| 玉屏| 阿克| 灵武市| 登封市| 揭东县| 平罗县| 香河县| 镇江市| 孝感市| 松潘县| 永川市|