国产探花免费观看_亚洲丰满少妇自慰呻吟_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>


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定兴县| 临夏市| 道孚县| 略阳县| 岚皋县| 蒙城县| 铅山县| 普格县| 马公市| 勃利县| 方山县| 磴口县| 延长县| 荥经县| 漳州市| 肇源县| 德保县| 桦南县| 鹤峰县| 本溪| 肥西县| 博爱县| 兴隆县| 广东省| 龙州县| 新兴县| 汉中市| 山阳县| 台山市| 松溪县| 镇原县| 合阳县| 安溪县| 万荣县| 苍溪县| 墨玉县| 博野县| 通州区| 余干县| 阿鲁科尔沁旗| 东阳市|