• Repeater、GirdView、DataList通用的分页代码


    前台代码:

    <div class="tggl_2">
            每页显示&nbsp;
            <asp:DropDownList ID="ddlPageSize" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlPageSize_SelectedIndexChanged">
                  <asp:ListItem Value="10">10</asp:ListItem>
                  <asp:ListItem Value="20">20</asp:ListItem>
                  <asp:ListItem Value="30">30</asp:ListItem>
                  <asp:ListItem Value="40">40</asp:ListItem>
                  <asp:ListItem Value="50">50</asp:ListItem>
           </asp:DropDownList>
             &nbsp;条&nbsp;&nbsp; 共&nbsp;<asp:Label ID="lblRecordCount" runat="server" Text=""></asp:Label>&nbsp;条信息&nbsp;&nbsp;
             第&nbsp;<asp:Label ID="lblPageIndex" runat="server" Text=""></asp:Label>&nbsp;页 /
             共&nbsp;<asp:Label ID="lblPageCount" runat="server" Text=""></asp:Label>&nbsp;页&nbsp;&nbsp;
              <asp:Button ID="btnFirst" runat="server" Text="首 页" OnClick="btnFirst_Click" />&nbsp;&nbsp;
              <asp:Button ID="btnPrev" runat="server" Text="上一页" OnClick="btnPrev_Click" />&nbsp;&nbsp;
              <asp:Button ID="btnNext" runat="server" Text="下一页" OnClick="btnNext_Click" />&nbsp;&nbsp;
              <asp:Button ID="btnLast" runat="server" Text="尾 页" OnClick="btnLast_Click" />&nbsp;&nbsp;
               转到第<input type="text" id="textfield" size="3" onkeyup="javascript:checkIsInt();"
                runat="server" />
                页
                <asp:Button ID="btnGoPageIndex" class="smallbutton" runat="server" Text="GO" Style="border: 0;
                 20px; height: 15px;" OnClick="btnGoPageIndex_Click" />&nbsp;&nbsp;
                <input name="chkall" type="checkbox" id="chkall" value="select" onclick="CheckAll(this.form)" /><label
                for="chkAll">全选</label>&nbsp;&nbsp;
                <asp:Button ID="btnDel" runat="server" OnClick="btnDel_Click" Text="批量删除" OnClientClick="return delIt()" />
     </div>

    代码生成如图所示:

    后台代码:

    #region 数据绑定及分页

            //分页控制显示方法
            public void contrlrepeater()
            {
                DataSet ds = (DataSet)ViewState["ds"];//绑定的数据集
                if (ds.Tables[0].Rows.Count > 0)
                {

          //分页设置并绑定数据
                    PagedDataSource pds = new PagedDataSource();
                    pds.DataSource = ds.Tables[0].DefaultView;
                    pds.AllowPaging = true;//运行分页
                    pds.PageSize = Convert.ToInt32(this.ddlPageSize.SelectedValue);//设置每页显示的条数
                    pds.CurrentPageIndex = Convert.ToInt32(this.lblPageIndex.Text) - 1;//设置当前页
                    Repeater1.DataSource = pds;
                    this.lblPageCount.Text = pds.PageCount.ToString();
                    this.lblPageIndex.Text = (pds.CurrentPageIndex + 1).ToString();
                    this.lblRecordCount.Text = ds.Tables[0].Rows.Count.ToString();
                    this.btnFirst.Enabled = true;
                    this.btnPrev.Enabled = true;
                    this.btnNext.Enabled = true;
                    this.btnLast.Enabled = true;
                    if (pds.CurrentPageIndex < 1)
                    {
                        this.btnFirst.Enabled = false;
                        this.btnPrev.Enabled = false;
                    }
                    if (pds.CurrentPageIndex == pds.PageCount - 1)
                    {
                        this.btnNext.Enabled = false;
                        this.btnLast.Enabled = false;
                    }
                    if (pds.PageCount == 1)
                    {
                        this.btnGoPageIndex.Enabled = false;
                    }
                    Repeater1.DataBind();
                }
                else
                {
                    this.lblPageCount.Text = "0";
                    this.lblPageIndex.Text = "0";
                    this.lblRecordCount.Text = "0";
                    this.ddlPageSize.Enabled = false;
                    this.btnFirst.Enabled = false;
                    this.btnPrev.Enabled = false;
                    this.btnNext.Enabled = false;
                    this.btnLast.Enabled = false;
                    this.btnGoPageIndex.Enabled = false;
                    this.btnDel.Enabled = false;
                    Repeater1.DataSource = null;
                    Repeater1.DataBind();
                }
            }
            //更改每页显示条数
            protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
            {
                this.lblPageIndex.Text = "1";
                this.contrlrepeater();
            }
            /// <summary>
            /// 首页
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void btnFirst_Click(object sender, EventArgs e)
            {
                this.lblPageIndex.Text = "1";
                this.contrlrepeater();
            }
            /// <summary>
            /// 上一页
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void btnPrev_Click(object sender, EventArgs e)
            {
                this.lblPageIndex.Text = Convert.ToString(Convert.ToInt32(this.lblPageIndex.Text) - 1);
                this.contrlrepeater();
            }
            /// <summary>
            /// 下一页
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void btnNext_Click(object sender, EventArgs e)
            {
                this.lblPageIndex.Text = Convert.ToString(Convert.ToInt32(this.lblPageIndex.Text) + 1);
                this.contrlrepeater();
            }
            /// <summary>
            /// 尾页
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void btnLast_Click(object sender, EventArgs e)
            {
                this.lblPageIndex.Text = this.lblPageCount.Text;
                this.contrlrepeater();
            }
            /// <summary>
            /// 跳转
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void btnGoPageIndex_Click(object sender, EventArgs e)
            {
                if (this.textfield.Value.Trim().ToString() != "")
                {
                    this.lblPageIndex.Text = this.textfield.Value;
                    this.contrlrepeater();
                }
            }
    #endregion

  • 相关阅读:
    【Other】申请免费的SSL证书及部署Https协议
    【MySql】mysql-5.7.20-winx64安装配置
    【CSharp】C#程序使用.NET Reactor进行混淆加壳
    【Linux】Ubuntu安装Python3
    【Linux】Ubuntu安装Googlepinyin中文输入法
    【Linux】Ubuntu修改默认源
    【C/S】FIPS安全验证问题
    【Android】apk文件反编译
    笔记之《用python写网络爬虫》
    memcache (持续了解ing...)
  • 原文地址:https://www.cnblogs.com/linling/p/3242704.html
Copyright © 2020-2023  润新知