实现DataList控件的分页
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 Content="Microsoft Visual Studio 7.0">
<meta Content="C#">
<meta content="Javascript">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form method="post" action="default.aspx" runat="server">
<table cellpadding="2" cellspacing="0">
<tr>
<td>
<asp:DataList RepeatColumns="1" Runat="server">
<ItemTemplate>
<table cellpadding="0" cellspacing="1">
<tr>
<td>
<table cellpadding="0" cellspacing="1">
<tr>
<td>
<table cellpadding="6" cellspacing="0">
<tr>
<td valign="top">
<table cellspacing="0" cellpadding="0">
<tr>
<td>
<table cellspacing="0" cellpadding="0">
<tr>
<td>
<div><font><%# DataBinder.Eval(Container.DataItem,"name") %></font></div>
</td>
</tr>
</table>
<div><font><img src="<%# DataBinder.Eval(Container.DataItem,"face") %>"></font><br>
<br>
★
<%# DataBinder.Eval(Container.DataItem,"sex") %>
★</div>
</td>
</tr>
</table>
</td>
<td>
<table cellpadding="0" cellspacing="0">
<tr>
<td></td>
</tr>
</table>
</td>
<td valign="top">
<table cellspacing="0" cellpadding="0">
<tr>
<td><img src="http://www.host01.com/Get/Net/000200011/images/icon/subject.gif" alt="主题">:<%# DataBinder.Eval(Container.DataItem,"caption") %><br>
</td>
<td><font> <img src="http://www.host01.com/Get/Net/000200011/images/icon/posttime.gif" alt="发表时间[北京时间]">:<%# DataBinder.Eval(Container.DataItem,"postdate") %></font></td>
</tr>
</table>
<hr size="0.1">
<table cellpadding="5" cellspacing="0">
<tr>
<td valign="top"><img src="http://www.host01.com/Get/Net/000200011/images/emot/1.gif">
</td>
<td valign="top">
<%# DataBinder.Eval(Container.DataItem,"content") %>
</td>
</tr>
</table>
<table cellpadding="4" cellspacing="0">
<tr>
<td valign="top">
<hr size="0.1">
<a href="<%# DataBinder.Eval(Container.DataItem,"homepage") %>" target="_blank"><img src="http://www.host01.com/Get/Net/000200011/images/icon/home.gif" border="0"></a>
<a href="http://www.chinagz.net/ip/ipsearch.cgi?ip=<%# DataBinder.Eval(Container.DataItem,"ip") %>" target="_blank"><img src="http://www.host01.com/Get/Net/000200011/images/icon/ip.gif"></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.host01.com/Get/Net/000200011/images/icon/qq.gif"></a>
<a href="mailto:<%# DataBinder.Eval(Container.DataItem,"email") %>"><img src="http://www.host01.com/Get/Net/000200011/images/icon/email.gif" alt=""></a>
<a href="javascript:;" admin","","hide","postcontent","","show")">
<img src="http://www.host01.com/Get/Net/000200011/images/icon/postguest.gif"></a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<BR>
</td>
</tr>
</table>
<table cellspacing="0" cellpadding="2">
<TBODY>
<tr>
<td>
<table cellpadding="1" cellspacing="0">
<tr>
<td>
<table cellspacing="0" cellpadding="0">
<tr>
<td>
共有<asp:Label ForeColor="red" runat="server" />条记录 当前为<asp:Label ForeColor="red" runat="server" />/<asp:Label ForeColor="red" runat="server" />页
</td>
<td>
<asp:LinkButton Text="上一页" CommandName="prev" runat="server" />
<asp:LinkButton 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();
}