DataList分页
1,前台页面:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="IConcern.aspx.cs" Inherits="IConcern" %>
<asp:Content ID="Content2" ContentPlaceHolderID="head" Runat="Server">
<link rel="stylesheet" href="css/content.css" type="text/css" />
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div class="myConcernlist_block"><ul>
<asp:DataList ID="myConcernlist" runat="server" RepeatColumns="6">
<ItemTemplate>
<li>
<div class="myConcernlist_img"><img src="<%#DataBinder.Eval(Container.DataItem, "photo")%>" class="myConcernlist_pic"/></div>
<div class="myConcernlist_name"><%#DataBinder.Eval(Container.DataItem, "truename")%></div>
<div class="myConcernlist_consern"><a href="#">取消</a></div>
</li>
</ItemTemplate>
</asp:DataList>
</ul></div>
<div class="paging">
当前页是:第<span class="current"><asp:Label ID="currentPage" runat="server" Text="1"></asp:Label></span>页,
总共有<asp:Label ID="total" runat="server"></asp:Label>页
<asp:LinkButton ID="first" runat="server" Text="第一页" OnCommand="LinkBotton_Click" CommandArgument="1" CommandName="first"></asp:LinkButton>
<asp:LinkButton ID="previous" runat="server" Text="上一页" OnCommand="LinkBotton_Click" CommandArgument="1" CommandName="previous"></asp:LinkButton>
<asp:LinkButton ID="next" runat="server" Text="下一页" OnCommand="LinkBotton_Click" CommandArgument="1" CommandName="next"></asp:LinkButton>
<asp:LinkButton ID="last" runat="server" Text="最后页" OnCommand="LinkBotton_Click" CommandArgument="1" CommandName="last"></asp:LinkButton>
</div>
</asp:Content>
2,后台页面:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class IConcern : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataList_DataBind();
}
}
public int totalpage;
protected void DataList_DataBind()
{
PagedDataSource ps = new PagedDataSource();
string sql_ds;
int curpage = Convert.ToInt32(this.currentPage.Text)-1;
//PagedDataSource的CurrentPageIndex是从0开始的,这也是为什么置为0而不是1的缘故。
sql_ds = "select a.*,u.truename,u.photo from individual_attention as a join individual_user as u on a.attention_id=u.id";
//myConcernlist.DataSource=Maticsoft.DBUtility.DbHelperSQL.Query(sql_ds).Tables[0].DefaultView;
ps.DataSource = Maticsoft.DBUtility.DbHelperSQL.Query(sql_ds).Tables[0].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 5;
ps.CurrentPageIndex = curpage;
first.Enabled = true;
previous.Enabled = true;
next.Enabled = true;
last.Enabled = true;
if (ps.CurrentPageIndex == 0)
{
this.first.Enabled = false;
this.previous.Enabled = false;
}
if (ps.CurrentPageIndex == ps.PageCount-1)
{
this.next.Enabled = false;
this.last.Enabled = false;
}
this.total.Text = Convert.ToString(ps.PageCount);
totalpage =ps.PageCount;
myConcernlist.DataSource = ps;
this.myConcernlist.DataKeyField = "id";
myConcernlist.DataBind();
}
protected void LinkBotton_Click(Object send,CommandEventArgs e)
{
string temp;
temp=e.CommandName;
if(temp=="first")
{
currentPage.Text =Convert.ToString(1);
}
if (temp == "previous")
{
currentPage.Text = Convert.ToString(Convert.ToInt32(currentPage.Text) - 1);
}
if (temp == "next")
{
currentPage.Text = Convert.ToString(Convert.ToInt32(currentPage.Text) + 1);
}
if (temp == "last")
{
currentPage.Text = Convert.ToString(totalpage);
}
DataList_DataBind();
}
}