• Web 分页


    以Car表为例分页

    页面以table展示数据分页 页面代码

     1  <asp:Repeater ID="Repeater1" runat="server">
     2  2             <HeaderTemplate>
     3  3                 <table style="background-color:navy;text-align:center;80%;">
     4  4                     <thead>
     5  5                         <tr style="color:white;">
     6  6                             <td>编号</td>
     7  7                             <td>名称</td>
     8  8                             <td>铭牌</td>
     9  9                             <td>日期</td>
    10 10                             <td>油耗</td>
    11 11                             <td>动力</td>
    12 12                             <td>轴距</td>
    13 13                             <td>价格</td>
    14 14                         </tr>
    15 15                     </thead>
    16 16                     <tbody>
    17 17             </HeaderTemplate>
    18 18             <ItemTemplate>
    19 19                 <tr style="background-color:white;">
    20 20                     <td><%#Eval("Code") %></td>
    21 21                     <td><%#Eval("Name") %></td>
    22 22                     <td><%#Eval("Brand") %></td>
    23 23                     <td><%#Eval("Time") %></td>
    24 24                     <td><%#Eval("Oil") %></td>
    25 25                     <td><%#Eval("Powers") %></td>
    26 26                     <td><%#Eval("Exhaust") %></td>
    27 27                     <td><%#Eval("Price") %></td>
    28 28                 </tr>
    29 29 
    30 30             </ItemTemplate>
    31 31             <FooterTemplate>
    32 32                         </tbody>
    33 33                 </table>
    34 34             </FooterTemplate>
    35 35         </asp:Repeater>
    36 36 
    37 37         当前第【<asp:Label ID="Label_now" runat="server" Text="Label"></asp:Label>】页&nbsp &nbsp 
    38 38         共【<asp:Label ID="Label_max" runat="server" Text="Label"></asp:Label>】页 &nbsp &nbsp
    39 39         <asp:LinkButton ID="LinkButton_frist" runat="server">首页</asp:LinkButton>&nbsp &nbsp
    40 40         <asp:LinkButton ID="LinkButton_prve" runat="server">上一页</asp:LinkButton>&nbsp &nbsp
    41 41         <asp:LinkButton ID="LinkButton_next" runat="server">下一页</asp:LinkButton>&nbsp &nbsp
    42 42         <asp:LinkButton ID="LinkButton_last" runat="server">尾页</asp:LinkButton>&nbsp &nbsp
    43 43         跳转到<asp:DropDownList ID="DropDownList_jump" AutoPostBack="true" runat="server"></asp:DropDownList>

    页面显示 

    分页关键:每页显示多少条数据 显示的是第几页数据,数据访问类分页查询方法

     1   public List<Car> Select(int pagecount,int pagenumber)
     2     {
     3         List<Car> clist = new List<Car>();
     4         cmd.CommandText = " select top " + pagecount + "* from Car where Code not in(select top " + (pagecount * (pagenumber - 1)) + " Code from Car);";
     5         conn.Open();
     6         SqlDataReader dr = cmd.ExecuteReader();
     7         if (dr.HasRows)
     8         {
     9             while (dr.Read())
    10             {
    11                 Car c = new Car();
    12                 c.Code = dr[0].ToString();
    13                 c.Name = dr[1].ToString();
    14                 c.Brand = dr[2].ToString();
    15                 c.Time = Convert.ToDateTime(dr[3]);
    16                 c.Oil = Convert.ToDecimal(dr[4]);
    17                 c.Powers = Convert.ToInt32(dr[5]);
    18                 c.Exhaust = Convert.ToInt32(dr[6]);
    19                 c.Price = Convert.ToDecimal(dr[7]);
    20                 clist.Add(c);
    21             }
    22         }
    23         conn.Close();
    24         return clist;
    25     }

    后台代码

      1 int pagecount = 4;//每页显示的数据条数
      2     protected void Page_Load(object sender, EventArgs e)
      3     {
      4         if (!IsPostBack)//加载页面展示数据
      5         {
      6             Repeater1.DataSource = new CarData().Select(pagecount,1);
      7             Repeater1.DataBind();
      8             Label_now.Text = "1";
      9             Label_max.Text = Maxpage().ToString();
     10             for (int i = 1; i <= Maxpage(); i++)//跳转下拉列表添加数据
     11             {
     12                 DropDownList_jump.Items.Add(new ListItem(i.ToString(), i.ToString()));
     13             }
     14 
     15         }
     16         LinkButton_frist.Click += LinkButton_frist_Click;//首页
     17         LinkButton_prve.Click += LinkButton_prve_Click;//上一页
     18         LinkButton_next.Click += LinkButton_next_Click;//下一页
     19         LinkButton_last.Click += LinkButton_last_Click;//尾页
     20         DropDownList_jump.SelectedIndexChanged += DropDownList_jump_SelectedIndexChanged;//跳转页面
     21         
     22     }
     23 void LinkButton_frist_Click(object sender, EventArgs e)//首页
     24     {   
     26         //数据绑定上去
     27         Repeater1.DataSource = new CarData().Select(pagecount, 1);
     28         Repeater1.DataBind();
     29         //当前页数改变
     30         Label_now.Text = "1";
     31         LinkButton_next.Enabled = true;
     32         LinkButton_prve.Enabled = false;    
     34     }
     35    public int Maxpage()//最大页数
     36     {
     37         List<Car> clist = new CarData().Select();
     38         return Convert.ToInt32(Math.Ceiling(clist.Count / (pagecount * 1.0)));
     39     }
     40 
     41     void LinkButton_prve_Click(object sender, EventArgs e)//上一页
     42     {
     43         // 显示第几页
     44         int pagenumber = Convert.ToInt32(Label_now.Text) -1;
     45         //数据绑定上去
     46         Repeater1.DataSource = new CarData().Select(pagecount, pagenumber);
     47         Repeater1.DataBind();
     48         //当前页数改变
     49         Label_now.Text = pagenumber.ToString();
     50         // 设置上下页按钮时候可用
     51         if (pagenumber <= 1)
     52         {
     53             LinkButton_prve.Enabled = false;
     54         }
     55         LinkButton_next.Enabled = true;
     56     }
     57 
     58     void LinkButton_next_Click(object sender, EventArgs e)//下一页
     59     {
     60         // 显示第几页
     61         int pagenumber = Convert.ToInt32(Label_now.Text) + 1;
     62         //数据绑定上去
     63         Repeater1.DataSource = new CarData().Select(pagecount, pagenumber);
     64         Repeater1.DataBind();
     65        //当前页数改变
     66         Label_now.Text = pagenumber.ToString();
     67             // 设置上下页按钮时候可用
     68         if (pagenumber >= Maxpage())
     69         {
     70             LinkButton_prve.Enabled = true;
     71         }
     72         LinkButton_next.Enabled = false;
     76     }
     77 
     78     void LinkButton_last_Click(object sender, EventArgs e)//尾页
     79     {   
     82         //数据绑定上去
     83         Repeater1.DataSource = new CarData().Select(pagecount, Maxpage());
     84         Repeater1.DataBind();
     85         //当前页数改变
     86         Label_now.Text = Maxpage().ToString();
     87         //按钮改变
     88         LinkButton_next.Enabled = false;
     89         LinkButton_prve.Enabled = true;
     92     }
     93 
     94 void DropDownList_jump_SelectedIndexChanged(object sender, EventArgs e)//跳转页面
     95     {
     96         //绑定数据
     97         Repeater1.DataSource = new CarData().Select(pagecount, Convert.ToInt32(DropDownList_jump.SelectedValue));
     98         Repeater1.DataBind();
     99         //显示当前 为第几页
    100         Label_now.Text = DropDownList_jump.SelectedValue;
    101         // 判断页数设置上下页按钮样式
    102         if (Convert.ToInt32(DropDownList_jump.SelectedValue) >= Maxpage())// 大于最大页数
    103         {
    104             LinkButton_next.Enabled = false;
    105             LinkButton_prve.Enabled = true;
    106         }
    107         else if (Convert.ToInt32(DropDownList_jump.SelectedValue) <= 1)//小于最小页数
    108         {
    109             LinkButton_next.Enabled = true;
    110             LinkButton_prve.Enabled = false;
    111         }
    112         else
    113         {
    114             LinkButton_next.Enabled = true;
    115             LinkButton_prve.Enabled = true;
    116         }
    118     }  
  • 相关阅读:
    QT调用其他UI并使用QLabel(text)
    QT调用单例模式脚本
    QT 调用另一个UI实现方式
    QT 键值
    (一) Mybatis 源码解析之源码概述
    设计模式之 模板模式开发
    十二、线程池
    (十一)并发容器ConcurrentHashMap
    mybatis plus 踩坑记 -- 自动填充
    C/C++ file
  • 原文地址:https://www.cnblogs.com/fuze/p/6249448.html
Copyright © 2020-2023  润新知