实现分页展示功能
基本功能:上一页、下一页、首页、尾页、跳转
两个重要的变量
1、每页显示几条数据
2、现在是第几页
方法
} /*表格样式*/ .tab { width: 100%; background-color: blue; text-align: center; } </style> </head> <body> <form id="form1" runat="server"> <div class="div1">奇点0216班学生信息</div> <div> <a href="zhuce.aspx" target="_blank">添加新同学</a> </div> <%--使用 Repeater 添加数据--%> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <%-- 头模板--%> <table class="tab"> <tr style="color: white; height: 30px;"> <td>编号</td> <td>用户名</td> <td>密码</td> <td>昵称</td> <td>性别</td> <td>生日</td> <td>年龄</td> <td>民族</td> <td>设置</td> </tr> </HeaderTemplate> <ItemTemplate> <%-- 项模板--%> <tr style="background-color: white;"> <td><%#Eval("Ids") %></td> <td><%#Eval("Username") %></td> <td><%#Eval("Password") %></td> <td><%#Eval("Nickname") %></td> <td><%#Eval("Sexstr") %></td> <td><%#Eval("Birthdaystr") %></td> <td><%#Eval("Age") %></td> <td><%#Eval("NationName") %></td> <td> <a href="xiugai.aspx?i=<%#Eval("Ids") %>">编辑 </a> <a οnclick="return confirm('是否要删除<%#Eval("NickName") %>?');" href="shanchu.aspx?i=<%#Eval("Ids") %>">删除</a> </td> </tr> </ItemTemplate> <FooterTemplate> <%--脚模板--%> </table> </FooterTemplate> </asp:Repeater> <br /><br /><br /> 当前第[ <asp:Label ID="Lab1" runat="server" Text="1"></asp:Label> ]页 共[ <asp:Label ID="Lab2" runat="server" Text="1"></asp:Label> ]页 <asp:Button ID="but_first" runat="server" Text="首页" /> <asp:Button ID="but_up" runat="server" Text="上一页" /> <asp:Button ID="but_down" runat="server" Text="下一页" /> <asp:Button ID="but_last" runat="server" Text="尾页" /> <asp:DropDownList ID="Dr1" runat="server"></asp:DropDownList> <asp:Button ID="but_jump" runat="server" Text="跳转" /> </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 { 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------ -- 与之相对应的委托事件
效果图
请发表评论