<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" %> <!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"> table { 100%; background-color:navy; text-align:center; font-family:微软雅黑; font-size:14px; } #tr_Head { color:white; } .tr_Main { background-color:#e0e0e0; } td { padding:15px; } #aaaa { 50px; } </style> </head> <body> <form id="form1" runat="server"> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table> <tr id="tr_Head"> <td>编号</td> <td>名称</td> <td>系别</td> <td>时间</td> <td>油耗</td> <td>马力</td> <td>排量</td> <td>价格</td> <td>操作</td> </tr> </HeaderTemplate> <ItemTemplate> <tr class="tr_Main"> <td><%#Eval("Code") %></td> <td><%#Eval("Name") %></td> <td><%#Eval("Brand") %></td> <td><%#Eval("Time") %></td> <td><%#Eval("Oil") %></td> <td><%#Eval("Powers") %></td> <td><%#Eval("Exhaust") %></td> <td><%#Eval("Price") %></td> <td>aaaa</td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater><br /> 当前第[<asp:Label ID="txt_PageCount" runat="server" Text="Label"></asp:Label>]页, <asp:LinkButton ID="Btn_First" runat="server">首页</asp:LinkButton> <asp:LinkButton ID="Btn_Prev" runat="server">上一页</asp:LinkButton> <asp:LinkButton ID="Btn_Next" runat="server">下一页</asp:LinkButton> <asp:LinkButton ID="Btn_Last" runat="server">末页</asp:LinkButton> <asp:DropDownList ID="aaaa" runat="server" AutoPostBack="true"></asp:DropDownList> </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 Default1 : System.Web.UI.Page { int Count = 5;//每页显示多少条 List<Car> query = new List<Car>(); protected void Page_Load(object sender, EventArgs e) { Btn_Prev.Click += Btn_Prev_Click;//上一页按钮 Btn_Next.Click += Btn_Next_Click;//下一页按钮 Btn_First.Click += Btn_First_Click;//首页 Btn_Last.Click += Btn_Last_Click;//末页 aaaa.SelectedIndexChanged += aaaa_SelectedIndexChanged; if (IsPostBack == false) { Repeater1.DataSource = new CarData().Select(Count, 1); Repeater1.DataBind(); txt_PageCount.Text = "1"; Btn_Prev.Enabled = false; Btn_First.Enabled = false; //添加下拉列表数据 for (int i = 1; i <= MaxNumber(); i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); aaaa.Items.Add(li); } } } void aaaa_SelectedIndexChanged(object sender, EventArgs e) { //取出来要去往的页数 int yy = Convert.ToInt32(aaaa.SelectedItem.Value); query = new CarData().Select(Count, yy); Repeater1.DataSource = query; Repeater1.DataBind(); txt_PageCount.Text = yy.ToString(); } //末页 void Btn_Last_Click(object sender, EventArgs e) { query = new CarData().Select(Count, MaxNumber()); Repeater1.DataSource = query; Repeater1.DataBind(); txt_PageCount.Text = MaxNumber().ToString(); Btn_Next.Enabled = false; Btn_Last.Enabled = false; Btn_First.Enabled = true; Btn_Prev.Enabled = true; } //首页 void Btn_First_Click(object sender, EventArgs e) { query = new CarData().Select(Count, 1); Repeater1.DataSource = query; Repeater1.DataBind(); txt_PageCount.Text = "1"; Btn_First.Enabled = false; Btn_Prev.Enabled = false; Btn_Last.Enabled = true; Btn_Next.Enabled = true; } //下一页按钮 void Btn_Next_Click(object sender, EventArgs e) { //先看看当前是第几页,然后+1 int NowNum = Convert.ToInt32(txt_PageCount.Text) + 1; query = new CarData().Select(Count, NowNum); Repeater1.DataSource = query; Repeater1.DataBind(); txt_PageCount.Text = NowNum.ToString(); //如果当前页数已经等于最大页数了,就将下一页按钮变为不可用 if (txt_PageCount.Text == MaxNumber().ToString()) { Btn_Next.Enabled = false; Btn_Last.Enabled = false; } Btn_Prev.Enabled = true; //上一页按钮变为可用 Btn_First.Enabled = true; //首页可用 } //上一页按钮 void Btn_Prev_Click(object sender, EventArgs e) { //先看看当前是第几页,然后-1 int NowNum = Convert.ToInt32(txt_PageCount.Text) - 1; query = new CarData().Select(Count, NowNum); Repeater1.DataSource = query; Repeater1.DataBind(); txt_PageCount.Text = NowNum.ToString(); //只要按下上一页按钮,就让下一页按钮变为可用 Btn_Next.Enabled = true; Btn_Last.Enabled = true; if (txt_PageCount.Text == "1") { Btn_Prev.Enabled = false; Btn_First.Enabled = false; } } /// <summary> /// 返回最大页数 /// </summary> /// <returns></returns> public int MaxNumber() { List<Car> list = new CarData().SelectAll();//全部数据的条数 double bb = list.Count / (Count * 1.0); //全部条数除以每页显示条数得到的就是最大页数 return Convert.ToInt32(Math.Ceiling(bb));//将最大页数取上限,返回出去 } }