zoukankan      html  css  js  c++  java
  • 使用Sql分页方法给Repeater控件分页的方法

    页面代码

    <div class="bookList">
    
        <asp:Repeater ID="rpBooks" runat="server">
    
            <HeaderTemplate>
    
                <ul class="title">
    
                    <li class="bName">书名</li>
    
                    <li class="bAuthor">作者</li>
    
                    <li class="bPrice">价格</li>
    
                </ul>
    
            </HeaderTemplate>
    
            <ItemTemplate>
    
                <ul class="data">
    
                    <li class="bName"><%#eval_r("title").ToString().Length > 20 ? eval_r("title").ToString().Substring(0, 20)+"..." : eval_r("title").ToString()%></li>
    
                    <li class="bAuthor"><%#eval_r("author").ToString().Length > 20 ? eval_r("author").ToString().Substring(0, 20) + "..." : eval_r("author").ToString()%></li>
    
                    <li class="bPrice"><%#eval_r("unitprice") %></li>
    
                </ul>
    
            </ItemTemplate>
    
            <AlternatingItemTemplate>
    
                 <ul class="data" style="background-color:Gray;">
    
                    <li class="bName"><%#eval_r("title").ToString().Length > 20 ? eval_r("title").ToString().Substring(0, 20)+"..." : eval_r("title").ToString()%></li>
    
                    <li class="bAuthor"><%#eval_r("author").ToString().Length > 20 ? eval_r("author").ToString().Substring(0, 20) + "..." : eval_r("author").ToString()%></li>
    
                    <li class="bPrice"><%#eval_r("unitprice") %></li>
    
                </ul>
    
            </AlternatingItemTemplate>
    
        </asp:Repeater>
    
    <div style="clear:both;"></div>
    
    <!--分页跳转-->
    
    <p class="page">
    
    <asp:LinkButton ID="lbtnFirst" CommandName="first" runat="server" oncommand="lbtnFirst_Command">首页</asp:LinkButton>
    
        <asp:LinkButton ID="lbtnPrevious" CommandName="previous" runat="server" oncommand="lbtnFirst_Command">上一页</asp:LinkButton>
    
        <asp:Label ID="lblPageIndex" runat="server" Text="1"></asp:Label>
    
        <asp:Label ID="Label3" runat="server" Text="/"></asp:Label>
    
        <asp:Label ID="lblPageSum" runat="server" Text=""></asp:Label>&nbsp;
    
        <asp:LinkButton ID="lbtnNext" CommandName="next" runat="server" oncommand="lbtnFirst_Command">下一页</asp:LinkButton>
    
        <asp:LinkButton ID="lbtnLast" CommandName="last" runat="server" oncommand="lbtnFirst_Command">尾页</asp:LinkButton>
    
        跳转到
    
        <asp:TextBox ID="txtPageIndex" runat="server" Width="30px"></asp:TextBox>
    
    <asp:Button ID="btnGo" runat="server" Text="跳转到" CommandName="btn"
    
            oncommand="lbtnFirst_Command" /><asp:Label ID="lblCount" runat="server" Text="Label"></asp:Label>
    
        条记录</p>
    
        </div>

    后置代码

     int index =1;//初始化当前页索引,默认显示第一页
        int pageSize = 5;//设置每页显示条数
        Common.Page page =null;
        protected void Page_Load(object sender, EventArgs e)
        {  
            //前段代码中给当前页标签默认为第一页,用标签保存当前页面索引
            index =int.Parse(lblPageIndex.Text);
            //在页面首次加载的时候,加载总页数,总记录数和信息列表
            if (!IsPostBack )
            {
                page = new Common.Page(index, pageSize);
                this.lblCount.Text = page.Count.ToString();
                this.lblPageSum.Text = page.PageSum.ToString();
                BookBind();
            }
        }
        //翻页按钮命令事件
        protected void lbtnFirst_Command(object sender, CommandEventArgs e)
        {
            page = new Common.Page(index, pageSize);
            string commandName = e.CommandName;
            switch (commandName)
            {
                case "first":
                    page.PageIndex = 1;
     
                    break;
                case "previous":
                    if (index==1)
                    {
                        page.PageIndex = 1;
                    }
                    else
                    {
                        page.PageIndex =index-1;
                    }
                    break;
                case "next":
                    if (index == page.PageSum)
                    {
                        page.PageIndex = page.PageSum;
                    }
                    else
                    {
                        page.PageIndex=index+1;
                    }
                    break;
                case "last":
                    page.PageIndex =page.PageSum;
                    break;
                case "btn":
                    page.PageIndex = int.Parse(txtPageIndex.Text);
                    break;
            }
            this.lblPageIndex.Text = page.PageIndex.ToString();
            BookBind();
        }
        //给Repeater控件绑定数据
        private void BookBind()
        {
            this.rpBooks.DataSource = page.CurPager;
            this.rpBooks.DataBind();
        }
        //获取记录总数的方法
        public int GetCount()
        {
            return page.Count;
        }
        //获取总页数的方法
        public int GetPageSum()
        {
            return page.PageSum;
        }
     

    注意事项

    本例使用label标签保存当前页面索引,也可以使用视图状态保存。

  • 相关阅读:
    Openwrt智能路由系统开发--内容总结
    EdgeX foundry树莓派搭建与MQTT通讯
    C语言学习截图保存
    使用python生成Latex公式语法
    数据结构与算法之排序
    数据结构与算法之常用数据结构
    MySQL数据库规范 (设计规范+开发规范+操作规范)
    动态规划之LIS(最长上升子序列)
    动态规划之区间DP
    动态规划之状态压缩DP
  • 原文地址:https://www.cnblogs.com/xyyt/p/3978871.html
Copyright © 2011-2022 走看看