Chapter 5. 分页
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// ChinaStates 的摘要说明 /// </summary> public class ChinaStates { public ChinaStates() { // // TODO: 在此处添加构造函数逻辑 // } private string _AreaCode; public string AreaCode { get { return _AreaCode; } set { _AreaCode = value; } } private string _AreaName; public string AreaName { get { return _AreaName; } set { _AreaName = value; } } private string _Pcode; public string Pcode { get { return _Pcode; } set { _Pcode = value; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; /// <summary> /// ChinaStatesData 的摘要说明 /// </summary> public class ChinaStatesData { SqlConnection _conn = null; SqlCommand _cmd = null; SqlDataReader _dr = null; public ChinaStatesData() { _conn = new SqlConnection("server=.; database=mydb; user=sa; pwd=ray;"); _cmd = _conn.CreateCommand(); } /// <summary> /// 查询ChinaStates全部数据 /// </summary> /// <returns></returns> public List<ChinaStates> Select() { List<ChinaStates> list = new List<ChinaStates>(); _cmd.CommandText = "select *from ChinaStates"; _conn.Open(); _dr = _cmd.ExecuteReader(); if (_dr.HasRows) { while (_dr.Read()) { ChinaStates cs = new ChinaStates() { AreaCode = _dr[0].ToString(), AreaName = _dr[1].ToString(), Pcode = _dr[2].ToString(), }; list.Add(cs); } } _conn.Close(); return list; } /// <summary> /// 分页查询ChinaStates数据 /// </summary> /// <param name="count">每页显示几条数据</param> /// <param name="number">第几页</param> /// <returns></returns> public List<ChinaStates> Select(int count,int Pagenumber) { List<ChinaStates> list = new List<ChinaStates>(); _cmd.CommandText = "select top " + count + " *from ChinaStates where AreaCode not in (select top "+(Pagenumber-1)*count+" AreaCode from ChinaStates)"; _conn.Open(); _dr = _cmd.ExecuteReader(); if (_dr.HasRows) { while (_dr.Read()) { ChinaStates cs = new ChinaStates() { AreaCode = _dr[0].ToString(), AreaName = _dr[1].ToString(), Pcode = _dr[2].ToString(), }; list.Add(cs); } } _conn.Close(); return list; } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <style type="text/css"> #td1 { background-color:darksalmon; width:50%; left:25%; position:relative; text-align:center; } #tr_head { color:white; } .tr_item { background-color:white; } #sign { position:relative; left:25%; } </style> </head> <body> <form id="form1" runat="server"> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table id="td1"> <tr id="tr_head"> <td>区域代码</td> <td>区域名称</td> <td>父级代码</td> </tr> </HeaderTemplate> <ItemTemplate> <tr class="tr_item"> <td><%#Eval("AreaCode") %></td> <td><%#Eval("AreaName") %></td> <td><%#Eval("PCode") %></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <div id="sign"> 当前第<<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>>页, 共<<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>>页 <asp:LinkButton ID="btn_home" runat="server">首页</asp:LinkButton> <asp:LinkButton ID="btn_pageup" runat="server">上一页</asp:LinkButton> <asp:LinkButton ID="btn_pagedown" runat="server">下一页</asp:LinkButton> <asp:LinkButton ID="btn_end" runat="server">尾页</asp:LinkButton> 跳转到<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"></asp:DropDownList>页 </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; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //页面刷新显示第一页数据 Repeater1.DataSource = new ChinaStatesData().Select(PageCount, 1); Repeater1.DataBind(); //当前第一页 Label1.Text = "1"; //共XX页 Label2.Text = Max().ToString(); //跳转下拉列表绑定 for (int i = 1; i <= Max(); i++) { ListItem li = new ListItem(i.ToString(),i.ToString()); DropDownList1.Items.Add(li); } } btn_home.Click += btn_home_Click; //首页点击事件 btn_pageup.Click += btn_pageup_Click; //上一页点击事件 btn_pagedown.Click += btn_pagedown_Click; //下一页点击事件 btn_end.Click += btn_end_Click; //尾页点击事件 DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged; //跳转页面 } //跳转页面 void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { int num = Convert.ToInt32(DropDownList1.SelectedItem.Text); Repeater1.DataSource = new ChinaStatesData().Select(PageCount, num); Repeater1.DataBind(); Label1.Text = num.ToString(); } //每页显示条数 int PageCount = 10; //尾页点击事件 void btn_end_Click(object sender, EventArgs e) { Repeater1.DataSource = new ChinaStatesData().Select(PageCount, Max()); Repeater1.DataBind(); Label1.Text = Max().ToString(); } //下一页点击事件 void btn_pagedown_Click(object sender, EventArgs e) { int num = Convert.ToInt32(Label1.Text); num++; if (num > Max()) { return; } Label1.Text = num.ToString(); Repeater1.DataSource = new ChinaStatesData().Select(PageCount, num); Repeater1.DataBind(); } //上一页点击事件 void btn_pageup_Click(object sender, EventArgs e) { int num = Convert.ToInt32(Label1.Text); num--; if (num <1) { return; } Label1.Text = num.ToString(); Repeater1.DataSource = new ChinaStatesData().Select(PageCount, num); Repeater1.DataBind(); } //首页点击事件 void btn_home_Click(object sender, EventArgs e) { Repeater1.DataSource = new ChinaStatesData().Select(PageCount, 1); Repeater1.DataBind(); Label1.Text = "1"; } /// <summary> /// 获取最大页数方法 /// </summary> /// <returns></returns> private int Max() { int count = new ChinaStatesData().Select().Count; double a = count / (PageCount * 1.0); return Convert.ToInt32(Math.Ceiling(a)); } }