<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList.aspx.cs" Inherits="TEST_DataList" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="score" runat="server" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="Gainsboro" EditItemStyle-BackColor="yellow"> <HeaderTemplate> <table width="100%" border="1"> <tr> <td align="center"> 用户ID </td> <td align="center"> 用户名 </td> <td align="center"> 密 码 </td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td align="center"> <%# DataBinder.Eval(Container.DataItem,"用户ID") %> </td> <td align="center"> <%# DataBinder.Eval(Container.DataItem,"用户名") %> </td> <td align="center"> <%# DataBinder.Eval(Container.DataItem,"密码") %> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:DataList> <asp:LinkButton ID="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" /> <asp:LinkButton ID="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" /> 共有<asp:Label ID="lblRecordCount" ForeColor="red" runat="server" />条记录 当前为<asp:Label ID="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label ID="lblPageCount" ForeColor="red" runat="server" />页 </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections; using System.Data; using System.Data.SqlClient; public partial class TEST_DataList : System.Web.UI.Page { //分页相关参数 int PageSize, RecordCount, PageCount, CurrentPage; SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=CSDN;Persist Security Info=True;User ID=sa;Password=sa;"); protected void Page_Load(object sender, EventArgs e) { //设定PageSize PageSize = 10; if (!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 SYS_USERINF"; con.Open(); SqlCommand cmd = new SqlCommand(strCount, con); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { intCount = Int32.Parse(dr["co"].ToString()); } else { intCount = 0; } dr.Close(); con.Close(); return intCount; } ICollection CreateSource() { int StartIndex; //设定导入的起终地址 StartIndex = CurrentPage * PageSize; string strSel = "select * from SYS_USERINF"; DataSet ds = new DataSet(); SqlDataAdapter sda = new SqlDataAdapter(strSel, con); sda.Fill(ds, StartIndex, PageSize, "Score"); return ds.Tables["Score"].DefaultView; } public void ListBind() { score.DataSource = CreateSource(); score.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(); } }
样式没有应用好的css,比较难看,但是分页功能就是如此实现的。上图: