C#分页功能 和大家分享

在Repeater控件内实现分页功能:

页面中代码

<asp:Repeater ID="RptJobList" runat="server" OnItemDataBound="RptJobList_ItemDataBound">

<FooterTemplate>

<td  align = "right" height="30" width = "828" colspan="2" style="font-size:10pt;color:#636363;">
            <asp:HyperLink colspan="2" style="font-size:10pt;color:#636363;" ID="hlfir" runat="server" Text="第一页"></asp:HyperLink>
                &nbsp;
            <asp:HyperLink colspan="2" style="font-size:10pt;color:#636363;" ID="hlp" runat="server" Text="上一页"></asp:HyperLink>
                &nbsp;
            <asp:HyperLink colspan="2" style="font-size:10pt;color:#636363;" ID="hln" runat="server" Text="下一页"></asp:HyperLink>
                &nbsp;
            <asp:HyperLink colspan="2" style="font-size:10pt;color:#636363;" ID="hlla" runat="server" Text="最后一页"></asp:HyperLink>
                &nbsp;&nbsp;&nbsp; 转到第


         <asp:DropDownList ID="ddlp" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlp_SelectedIndexChanged" >
         </asp:DropDownList>页
</td>

</FooterTemplate>
</asp:Repeater>

 

.CS代码:

数据绑定函数[自己补充完整即可]

PagedDataSource pds = new PagedDataSource();
                pds.DataSource = dt.DefaultView;
                pds.AllowPaging = true;//允许分页
                pds.PageSize = 20;//单页显示项数
                pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);

 

        /// <summary>
        /// Repeater控件分页 处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RptJobList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                DropDownList ddlp = (DropDownList)e.Item.FindControl("ddlp");

                HyperLink lpfirst = (HyperLink)e.Item.FindControl("hlfir");
                HyperLink lpprev = (HyperLink)e.Item.FindControl("hlp");
                HyperLink lpnext = (HyperLink)e.Item.FindControl("hln");
                HyperLink lplast = (HyperLink)e.Item.FindControl("hlla");
                PagedDataSource ps = pds();
                string a1 = Request.QueryString["page"];
                ps.CurrentPageIndex = Convert.ToInt32(a1);

                int n = Convert.ToInt32(ps.PageCount);//n为分页数
                int i = Convert.ToInt32(ps.CurrentPageIndex);//i为当前页

                Label lblpc = (Label)e.Item.FindControl("lblpc");

                //if (!IsPostBack)
                //{
                    for (int j = 0; j < n; j++)
                    {
                        ddlp.Items.Add(Convert.ToString(j + 1));
                    }
                //}

                    string addUrl = "&&testType=" + ddlWorkType.SelectedValue + "&&provinceId=" + ddlProvince.SelectedValue + "&&jobclassId" + ddlJobclass.SelectedValue;
                if (i <= 0)
                {
                    lpfirst.Enabled = false;
                    lpprev.Enabled = false;
                    lplast.Enabled = true;
                    lpnext.Enabled = true;
                }
                else
                {
                    lpprev.NavigateUrl = "?page=" + (i - 1) + addUrl;
                }
                if (i >= n - 1)
                {
                    lpfirst.Enabled = true;
                    lplast.Enabled = false;
                    lpnext.Enabled = false;
                    lpprev.Enabled = true;
                }
                else
                {
                    ViewState["i"] = i;
                    lpnext.NavigateUrl = "?page=" + (i + 1) + addUrl;
                }

                lpfirst.NavigateUrl = "?page=0" + addUrl; //向本页传递参数page
                lplast.NavigateUrl = "?page=" + (n - 1) + addUrl;

                ddlp.SelectedIndex = Convert.ToInt32(ps.CurrentPageIndex);//更新下拉列表框中的当前选中页序号
            }
        }

        /// <summary>
        /// 转到第N页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlp_SelectedIndexChanged(object sender, EventArgs e)
        {
            //脚模板中的下拉列表框更改时激发
            string pg = Convert.ToString((Convert.ToInt32(((DropDownList)sender).SelectedValue) - 1));//获取列表框当前选中项
            ViewState["pageCount"] = pg;//
            Response.Redirect("parttime_joblist.aspx?page=" + pg + "&&testType=" + ddlWorkType.SelectedValue + "&&provinceId=" + ddlProvince.SelectedValue + "&&jobclassId" + ddlJobclass.SelectedValue); //页面转向
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值