實現DataList控制項的分頁

來源:互聯網
上載者:User
分頁|控制項 DataList控制項功能很強大,他支援選擇、編輯,實現的方法也很簡單,不過最令人頭疼的就是它不像DataGrid控制項一樣內建了分頁的功能,這麼好的一個控制項竟然不能分頁!!!確實是一個很讓人頭疼的事情。
  不過,只是DataList沒有提供內建的分頁功能,但是並不表示,我們不能使用DataList控制項來實現分頁,既然它不給我分頁功能,那隻好自己動手了。
  下面是全部原代碼,其實用到的方法和PHP中的分頁差不多,只是這裡用的是DataAdapter與DataSet組合,而不是PHP中的SQL語句直接搞定。



default.aspx檔案:
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="guestbook._default" %>
<HTML>
<HEAD>
<title>中國BS網留言簿</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 style="LEFT: 0px; WIDTH: 100%; WORD-WRAP: break-word; 130; 0pt"><%# DataBinder.Eval(Container.DataItem,"name") %></font></div>
</td>
</tr>
</table>
<div align="center"><font style="LEFT: 0px; WIDTH: 100%; WORD-WRAP: break-word; 130; 0pt"><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" style="TABLE-LAYOUT: fixed" 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 style="0pt">  <img src="http://www.163design.net/n/i/images/icon/posttime.gif" width="16" height="15" alt="發表時間[北京時間]">:<%# 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%" style="TABLE-LAYOUT: fixed">
<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%" style="LEFT: 0px; WIDTH: 100%; WORD-WRAP: break-word; 130; 0pt">
<%# 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> 
<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> 
<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> 
<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> 
<a href="javascript:;" 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" />條記錄  當前為<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;
//設定匯入的起終地址
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();
}

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。