前台HTML代码:
<%@ 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> </head> <body> <form id="form1" runat="server"> <div> </div> <h1>分页</h1> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor="#6633CC"> <tr> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">代号</td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">姓名</td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">系列</td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">上市时间</td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">油耗</td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">功率</td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">排量</td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">价格</td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF">图片</td> </tr> </HeaderTemplate> <FooterTemplate> </table> </FooterTemplate> <ItemTemplate> <table width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor="#6633CC"> <tr> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Code") %></td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Name") %></td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Brand") %></td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Time") %></td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Oil") %></td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Powers") %></td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Exhaust") %></td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Price") %></td> <td width="120" heigh="30" align="center" valign="middle" bgcolor="#FFFFFF"><%#Eval("Pic") %></td> </tr> </ItemTemplate> </asp:Repeater> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上一页" /> <asp:Button ID="Button2" runat="server" Text="下一页" OnClick="Button2_Click" /> <asp:Label ID="Label2" runat="server" Text="跳转到"></asp:Label> <asp:TextBox ID="TextBox1" runat="server" Width="37px"></asp:TextBox> <asp:Label ID="Label3" runat="server" Text="页"></asp:Label> <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="跳转" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:Literal ID="Literal1" runat="server"></asp:Literal> </form> </body> </html>
后台C#代码:
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) { //第一次页面加载 给session附上值 Session["ys"] = 1; TestDataContext context = new TestDataContext(); ////查汽车表 取前五条数据 Skip()跳过几条,Take()取几条 // Repeater1.DataSource = context.Car.Skip(0).Take(5); //要调过的条数 //先取出session里面的值 当前页数减去一 乘以五 TiaoXhuan(1); } Literal1.Text = ""; } protected void Button1_Click(object sender, EventArgs e) { //点上一页 先把session里面的值取出来 转成整型 int ys = Convert.ToInt32(Session["ys"]); if (ys > 1) { TestDataContext context = new TestDataContext(); //点了上一页 当前页数减一 ys = ys - 1; Session["ys"] = ys; TiaoXhuan(ys); } else { Literal1.Text="<script type=‘text/javascript‘>alert(‘当前已是第一页‘)</script>"; } } protected void Button2_Click(object sender, EventArgs e) { //下一页 先取出session里面的值 int ys = Convert.ToInt32(Session["ys"]); TestDataContext context = new TestDataContext(); //先判断是不是最后一页 去除所有的条数 除去每一页的条数5 看得到的数是多少 //int all = context.Car.Count();//取出的条数 // int zys; // if (all % 5 == 0)//得到的总页数 // { // zys = all / 5; // } // else // { // zys = all / 5 + 1; // } int zys = ALLYS(); if (ys < zys) { ys = ys + 1; Session["ys"] = ys; TiaoXhuan(ys); } else { Literal1.Text = "<script type=‘text/javascript‘>alert(‘当前已是最后一页‘)</script>"; } } //做函数 调用 实现跳转页面 给一个参数 跳到第几页 public void TiaoXhuan(int ys) { TestDataContext context = new TestDataContext(); int qu = (ys - 1) * 5; //计算出跳到第几页 Repeater1.DataSource = context.Car.Skip(qu).Take(5); Repeater1.DataBind(); //lable1里面要显示 当前第几页 总共第几页 int all = ALLYS(); Label1.Text = "当前第" + ys + "页,总共:"+all+"页"; } public int ALLYS()//计算出总页数 { TestDataContext context = new TestDataContext(); int all = context.Car.Count(); int zys; if (all % 5 == 0) { zys = all / 5; } else { zys = all / 5 + 1; } return zys; } protected void Button3_Click(object sender, EventArgs e) { int ys = 1; try { ys = Convert.ToInt32(TextBox1.Text); } catch (Exception) { Literal1.Text = "<script type=‘text/javascript‘>alert(‘请输入数字!‘)</script>"; } //输入页数 跳转到多少页 //int ys = Convert.ToInt32(TextBox1.Text); int all = ALLYS(); if (1 <= ys && ys <= all) { //调用跳转函数 TiaoXhuan(ys); //把session里面的值替换一下 Session["ys"] = ys; } else { Literal1.Text = "<script type=‘text/javascript‘>alert(‘超出范围!‘)</script>"; } } }
网页显示:
时间: 2024-10-12 04:46:18