实现分页展示功能
基本功能:上一页、下一页、首页、尾页、跳转
两个重要的变量
1、每页显示几条数据
2、现在是第几页
方法
} /*表格样式*/ .tab { width: 100%; background-color: blue; text-align: center; }
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{ int PageCount = 10; //每页显示条数 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //只在第一次加载时执行的代码 //数据展示时不需要,数据展示要展示最新数据 Repeater1.DataSource = new usersData().selecttop(PageCount, 1); Repeater1.DataBind(); Lab2.Text = pageAll().ToString(); //总页数 for (int i = 1; i <= pageAll(); i++) { ListItem li = new ListItem(); li.Text = i.ToString (); li.Value = i.ToString (); Dr1.Items.Add(li); } } but_first.Click += but_first_Click; but_last.Click += but_last_Click; but_up.Click += but_up_Click; but_down.Click += but_down_Click; but_jump.Click += but_jump_Click; } //跳转 void but_jump_Click(object sender, EventArgs e) { int lab =Convert .ToInt32 ( Dr1.SelectedValue); Repeater1.DataSource = new usersData().selecttop(PageCount, lab); Repeater1.DataBind(); Lab1.Text = lab.ToString(); } //下一页 void but_down_Click(object sender, EventArgs e) { //获取当前页,计算下一页 int lab = Convert.ToInt32(Lab1.Text) + 1; if (lab > pageAll()) { return; } //绑定下一页数据 Repeater1.DataSource = new usersData().selecttop(PageCount, lab); Repeater1.DataBind(); //修改当前显示的页数 Lab1.Text = lab.ToString(); } //上一页 void but_up_Click(object sender, EventArgs e) { //获取当前页,计算上一页 int lab = Convert.ToInt32(Lab1.Text) - 1; if (lab < 1) { return; } //绑定下一页数据 Repeater1.DataSource = new usersData().selecttop(PageCount, lab); Repeater1.DataBind(); //修改当前显示的页数 Lab1.Text = lab.ToString(); } //尾页 void but_last_Click(object sender, EventArgs e) { Repeater1.DataSource = new usersData().selecttop(PageCount, pageAll()); Repeater1.DataBind(); Lab1.Text = pageAll().ToString(); } //首页 void but_first_Click(object sender, EventArgs e) { Repeater1.DataSource = new usersData().selecttop(PageCount, 1); Repeater1.DataBind(); Lab1.Text = "1"; } //计算有多少页 public int pageAll() { int a = 0; int All = new usersData().SelectCount(); decimal d = Convert.ToDecimal(All) / PageCount; //int 除以 int 会自动去掉小数点, a = Convert.ToInt32(Math.Ceiling(d)); //取上限 return a; }}
AutoPostBack="true" --自动提交
DropDownList1.SelectedIndexChanged------ -- 与之相对应的委托事件
效果图