• WebForm(分页)


    public partial class _Default : System.Web.UI.Page
    {
        int num = 5;
        protected void Page_Load(object sender, EventArgs e)
        {
            int page = Convert.ToInt32(nowpage.Text);
           
                
            if (!IsPostBack)
            {
                Repeater1.DataSource = new CarData().SelectAll(num,page);
                Repeater1.DataBind();
    
                sumpage.Text = Maxpage().ToString();
                bt_first.Enabled = false;
                bt_up.Enabled = false;
    
                for (int i = 1; i <= Maxpage(); i++)
                {
                    ListItem li = new ListItem(i.ToString(),i.ToString());
                    Dr_page.Items.Add(li);
                }
            }
            bt_last.Click += bt_last_Click;
            bt_next.Click += bt_next_Click;
            bt_up.Click += bt_up_Click;
            bt_first.Click += bt_first_Click;
            bt_go.Click += bt_go_Click;
        }
        //跳转按钮
        void bt_go_Click(object sender, EventArgs e)
        {
            bt_first.Enabled = true;
            bt_up.Enabled = true;
            bt_next.Enabled = true;
            bt_last.Enabled = true;
            int a = Convert.ToInt32(Dr_page.SelectedValue);
            Repeater1.DataSource = new CarData().SelectAll(num, a);
            Repeater1.DataBind();
            //显示当前页数
            nowpage.Text = a.ToString();
            if (a == 1)
            {
                bt_first.Enabled = false;
                bt_up.Enabled = false;
            }
            if (a == Maxpage())
            {
                bt_next.Enabled = false;
                bt_last.Enabled = false;
            }
        }
        //首页按钮
        void bt_first_Click(object sender, EventArgs e)
        {
            bt_last.Enabled = true;
            bt_next.Enabled = true;
            Repeater1.DataSource = new CarData().SelectAll(num, 1);
            Repeater1.DataBind();
            //显示当前页数
            nowpage.Text ="1";
            bt_up.Enabled = false;
        }
        //上一页
        void bt_up_Click(object sender, EventArgs e)
        {
            //获取下一页查询页数
            int page = Convert.ToInt32(nowpage.Text);
            int uppage = page -1;
           
            //执行查询语句
            Repeater1.DataSource = new CarData().SelectAll(num, uppage);
            Repeater1.DataBind();
            //显示当前页数
            nowpage.Text = uppage.ToString();
            //上一页按钮不可用
            if (uppage == 1)
            {
                bt_first.Enabled = false;
                bt_up.Enabled = false;
            }
            bt_next.Enabled = true;
            bt_last.Enabled = true;
        }
        //下一页
        void bt_next_Click(object sender, EventArgs e)
        {
            //上一页按钮可用
            bt_first.Enabled = true;
            bt_up.Enabled = true;
            //获取下一页查询页数
            int page = Convert.ToInt32(nowpage.Text);
            int nextpage = page + 1;
            //下一页按钮不可用
            if (nextpage==Maxpage())
            {
                bt_last.Enabled = false;
                bt_next.Enabled = false;           
            }
            //执行查询语句
            Repeater1.DataSource = new CarData().SelectAll(num, nextpage);
            Repeater1.DataBind();
            //显示当前页数
            nowpage.Text = nextpage.ToString();       
            
        }
        //尾页
        void bt_last_Click(object sender, EventArgs e)
        {
            bt_first.Enabled = true;
            bt_up.Enabled = true;
            Repeater1.DataSource = new CarData().SelectAll(num, Maxpage());
            Repeater1.DataBind();
            //显示当前页数
            nowpage.Text = Maxpage().ToString();
            bt_next.Enabled = false;
            
        }
        //总页数方法
        public int Maxpage()
        {
            int end = 0;
            int item = new CarData().SelectCount();
            decimal a = Convert.ToDecimal(item) / num;
            end = Convert.ToInt32(Math.Ceiling(a));
            return end;
        }
    }
    分页.cs
    <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">
    
            <table style="100%;background-color:blue;text-align:center;">
                <tr style="color:white;">
                    <td>编号</td>
                    <td>名称</td>
                    <td>油耗</td>
                    <td>马力</td>
                    <td>排量</td>
                    <td>价格</td>
                </tr>
                <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <tr style="background-color:white;">
                            <td><%#Eval("Ids") %></td>
                            <td><%#Eval("Name") %></td>
                            <td><%#Eval("Oil") %></td>
                            <td><%#Eval("Powers") %></td>
                            <td><%#Eval("Exhaust") %></td>
                            <td><%#Eval("Price") %></td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </table>
            第[<asp:Label ID="nowpage" runat="server" Text="1"></asp:Label>]页&nbsp;&nbsp;
            共[<asp:Label ID="sumpage" runat="server" Text="1"></asp:Label>]页 &nbsp;&nbsp;
            <asp:Button ID="bt_first" runat="server" Text="首页" />
            <asp:Button ID="bt_up" runat="server" Text="上一页" />
            <asp:Button ID="bt_next" runat="server" Text="下一页" />
            <asp:Button ID="bt_last" runat="server" Text="尾页" />
    
            &nbsp;&nbsp;
            <asp:DropDownList ID="Dr_page" runat="server"></asp:DropDownList>
            <asp:Button ID="bt_go" runat="server" Text="跳转" />
        </form>
    </body>
    </html>
    分页.aspx
  • 相关阅读:
    繁星
    [转] android 中 任务、进程和线程的区别
    Android read-only file system解决方法
    [转] 用管道获得shell 命令的输出
    [转] Bound Service的三种方式(Binder、 Messenger、 AIDL)
    [转] Android LocalService与RemoteService理解
    Android 中的 Service 全面总结
    [转] 英语连接词~很全的版本!!!
    Questioning
    大数据笔记
  • 原文地址:https://www.cnblogs.com/hclyz/p/6898866.html
Copyright © 2020-2023  润新知