本文章的DataList控件的分页是用PageDataSource类写的
前台:
<asp:DataList ID="DL_Message" runat="server" Width="560px" OnItemDataBound="DL_Message_ItemDataBound" OnItemCommand="DL_Message_ItemCommand">
<HeaderTemplate>
<hr />
</HeaderTemplate>
<ItemTemplate>
<table style=" 536px">
<tr >
<td align="left">
主题:<%#DataBinder.Eval(Container.DataItem,"msg_theme")%></td>
</tr>
<tr>
<td align="center" >
<%#DataBinder.Eval(Container.DataItem,"msg_content") %>
</td>
</tr>
<tr >
<td align="center" >
<%#DataBinder.Eval(Container.DataItem, "msg_content")%>
</td>
</tr>
<tr >
<td align="right" style="height: 16px" >留言人:<%#DataBinder.Eval(Container.DataItem,"msg_name") %>
</td>
</tr>
<tr >
<td align="right" >
留言时间:<%#DataBinder.Eval(Container.DataItem,"AddDate") %>
</td>
</tr>
</table>
<hr/>
</ItemTemplate>
<FooterTemplate>
<table style="536px;">
<tr><td style="text-align:left; height: 36px;"><asp:ImageButton ID="pro" runat="server" ImageUrl="~/images/s3.gif" CommandName="pro" /> <asp:ImageButton ID="next" ImageUrl="~/images/s4.gif" runat="server" CommandName="next" />
共<asp:Label ID="count" runat="server" Width="15px"></asp:Label>页 第<asp:Label ID="num" runat="server" Width="34px"></asp:Label>页
</td></tr>
</table>
</FooterTemplate>
</asp:DataList>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
protected static PagedDataSource Pds;
class DataList分页
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillData();
}
}
static PagedDataSource Pds;
protected void bindborder()
{
string selectstr = "select * from Messages";
DataSet ds = DB.getDataset(selectstr);
Pds = new PagedDataSource();
Pds.DataSource = ds.Tables[0].DefaultView;
Pds.AllowPaging = true;
Pds.PageSize = 3;
this.DL_Message.DataSource = Pds;
this.DL_Message.DataBind();
}
protected void DL_Message_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
Label lbl = new Label();
lbl = (Label)e.Item.FindControl("count");
int a = Pds.Count;
lbl.Text = Pds.PageCount.ToString() ;
lbl=(Label)e.Item.FindControl("num");
lbl.Text =Convert.ToString(Pds.CurrentPageIndex + 1);
}
}
protected void DL_Message_ItemCommand(object source, DataListCommandEventArgs e)
{
//上一页
if (e.CommandName == "pro")
{
if (Pds.CurrentPageIndex > 0)
{
Pds.CurrentPageIndex--;
}
else
{
return;
}
this.DL_Message.DataSource = Pds;
this.DL_Message.DataBind();
}
else if(e.CommandName=="next")
{
if (Pds.CurrentPageIndex <Pds.PageCount-1)
{
Pds.CurrentPageIndex++;
}
else
{
return;
}
this.DL_Message.DataSource = Pds;
this.DL_Message.DataBind();
}
}
}