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

首頁 > 開發(fā) > 綜合 > 正文

實現(xiàn)DataList控件的分頁

2024-07-21 02:24:15
字體:
供稿:網(wǎng)友
datalist控件功能很強大,他支持選擇、編輯,實現(xiàn)的方法也很簡單,不過最令人頭疼的就是它不像datagrid控件一樣內(nèi)置了分頁的功能,這么好的一個控件竟然不能分頁!!!確實是一個很讓人頭疼的事情。
  不過,只是datalist沒有提供內(nèi)置的分頁功能,但是并不表示,我們不能使用datalist控件來實現(xiàn)分頁,既然它不給我分頁功能,那只好自己動手了。
  下面是全部原代碼,其實用到的方法和php中的分頁差不多,只是這里用的是dataadapter與dataset組合,而不是php中的sql語句直接搞定。



default.aspx文件:
<%@ page language="c#" codebehind="default.aspx.cs" autoeventwireup="false" inherits="guestbook._default" %>
<html>
<head>
<title>中國bs網(wǎng)留言簿</title>
<html>
<head>
<title>datalist分頁</title>
<meta name="generator" content="microsoft visual studio 7.0">
<meta name="code_language" content="c#">
<meta name="vs_defaultclientscript" content="javascript">
</head>
<body ms_positioning="gridlayout">
<form method="post" action="default.aspx" runat="server" id="form1">
<table width="100%" border="0" cellpadding="2" cellspacing="0" align="center">
<tr>
<td align="middle">
<asp:datalist id="myguest" repeatcolumns="1" runat="server">
<itemtemplate>
<table class="gueststyleline" border="0" cellpadding="0" cellspacing="1" align="center" width="610">
<tr>
<td>
<table class="gueststyleline" border="0" cellpadding="0" cellspacing="1" align="center" width="610">
<tr>
<td>
<table width="100%" border="0" cellpadding="6" cellspacing="0" class="gueststylebg" align="center">
<tr>
<td width="112" align="center" height="153" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%" class="gueststylebg">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0" align="center" height="25">
<tr>
<td class="filter">
<div align="center"><font ><%# databinder.eval(container.dataitem,"name") %></font></div>
</td>
</tr>
</table>
<div align="center"><font ><img src="<%# databinder.eval(container.dataitem,"face") %>" width="80" height="90" align="middle"></font><br>
<br>

<%# databinder.eval(container.dataitem,"sex") %>
★</div>
</td>
</tr>
</table>
</td>
<td align="center" height="153" width="2">
<table class="gueststyleline" width="1" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="1"></td>
</tr>
</table>
</td>
<td height="33" valign="top" width="463">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="gueststylebg" height="20">
<tr>
<td height="14" width="310"><img src="http://www.163design.net/n/i/images/icon/subject.gif" width="15" height="15" alt="主題">:<%# databinder.eval(container.dataitem,"caption") %><br>
</td>
<td width="151" height="14"><font > &nbsp;<img src="http://www.163design.net/n/i/images/icon/posttime.gif" width="16" height="15" alt="發(fā)表時間[北京時間]">:<%# databinder.eval(container.dataitem,"postdate") %></font></td>
</tr>
</table>
<hr class="gueststylehr" size="0.1" width="100%">
<table width="100%" border="0" cellpadding="5" cellspacing="0" class="gueststylebg" height="45%" >
<tr>
<td valign="top" width="28" align="center"><img src="http://www.163design.net/n/i/images/emot/1.gif" width="16" height="16">
</td>
<td valign="top" width="94%" >
<%# databinder.eval(container.dataitem,"content") %>
</td>
</tr>
</table>
<table width="100%" border="0" cellpadding="4" cellspacing="0">
<tr>
<td valign="top">
<hr class="gueststylehr" size="0.1" width="100%">
<a href="<%# databinder.eval(container.dataitem,"homepage") %>" target="_blank"><img src="http://www.163design.net/n/i/images/icon/home.gif" width="44" height="16"  border="0"></a>&nbsp;
<a href="http://www.chinagz.net/ip/ipsearch.cgi?ip=<%# databinder.eval(container.dataitem,"ip") %>" target="_blank"><img src="http://www.163design.net/n/i/images/icon/ip.gif" align="absmiddle" height="16" border="0"></a>&nbsp;
<a href="http://search.tencent.com/cgi-bin/friend/user_show_info?ln=<%# databinder.eval(container.dataitem,"qq") %>" target="_blank">
<img src="http://www.163design.net/n/i/images/icon/qq.gif" border="0" width="35" height="16"></a>&nbsp;
<a href="mailto:<%# databinder.eval(container.dataitem,"email") %>"><img src="http://www.163design.net/n/i/images/icon/email.gif" width="42" height="16" border="0" alt=""></a>&nbsp;
<a href="javascript:;" onclick="mm_showhidelayers("admin","","hide","postcontent","","show")">
<img src="http://www.163design.net/n/i/images/icon/postguest.gif" width="45" height="16" border="0"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</itemtemplate>
</asp:datalist>
<br>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="2" align="center" height="9">
<tbody>
<tr>
<td align="middle">
<table class="gueststyleline" width="610" border="0" cellpadding="1" cellspacing="0" height="18">
<tr>
<td height="24">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="gueststylebg" height="18">
<tr>
<td width="35%" align="middle" height="36">
共有<asp:label id="lblrecordcount" forecolor="red" runat="server" />條記錄&nbsp; 當(dāng)前為<asp:label id="lblcurrentpage" forecolor="red" runat="server" />/<asp:label id="lblpagecount" forecolor="red" runat="server" />頁
</td>
<td align="middle" height="36">
<asp:linkbutton id="lbnprevpage" text="上一頁" commandname="prev" runat="server" />
<asp:linkbutton id="lbnnextpage" text="下一頁" commandname="next" runat="server" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>



注意:必須把兩個linkbutton控件的command事件改為page_onclick:



codebehind文件:
private void page_load(object sender, system.eventargs e)
{
pagesize=1;
conn= new sqlconnection(application["guest_conn"].tostring());
conn.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 guest";
sqlcommand mycomm = new sqlcommand (strcount,conn);
sqldatareader dr = mycomm.executereader();
if(dr.read())
{
   intcount = int32.parse(dr["co"].tostring());
}
else
{
   intcount = 0;
}
dr.close();
return intcount;
}



icollection createsource()
{
int startindex;
//設(shè)定導(dǎo)入的起終地址
startindex = currentpage*pagesize;
string strsel = "select * from guest order by postdate desc";
dataset ds = new dataset();
sqldataadapter myadapter = new sqldataadapter(strsel,conn);
myadapter.fill(ds,startindex,pagesize,"guest");
return ds.tables["guest"].defaultview;
}



public void listbind()
{
myguest.datasource = createsource();
myguest.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();
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 邓州市| 壤塘县| 东乡县| 扶风县| 延安市| 搜索| 铜梁县| 梁河县| 石狮市| 香港| 合川市| 宣武区| 肃南| 象州县| 嘉黎县| 新宾| 商南县| 阿荣旗| 台前县| 神木县| 天等县| 陇川县| 紫云| 江川县| 尼木县| 巧家县| 湖口县| 伊吾县| 社会| 开原市| 曲水县| 恩施市| 乐陵市| 大足县| 阜平县| 中宁县| 大关县| 阳高县| 南乐县| 邹城市| 锡林郭勒盟|