Repeater、GirdView、DataList通用的分页代码_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > Repeater、GirdView、DataList通用的分页代码

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

 2013/8/7 13:08:21  林玲  博客园  我要评论(0)
  • 摘要:前台代码:<divclass="tggl_2">每页显示&nbsp;<asp:DropDownListID="ddlPageSize"runat="server"AutoPostBack="true"OnSelectedIndexChanged="ddlPageSize_SelectedIndexChanged"><asp:ListItemValue="10">10</asp:ListItem><asp
  • 标签:Repeater view list 代码

前台代码:

<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;
            width: 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

 

发表评论
用户名: 匿名