repeater控件自定义Url分页带参数

repeater控件的效果图如下:

该页面实现的功能如下:

1.上下分页,(也可以带首页和末页,我只是禁掉了没用)

2.根据用户输入的指定分页索引进行跳转

3.根据筛选数据的参数进行URL分页的参数传递

4.数据的导出功能

前台代码:

<!--表格具体内容-->
                    <div class="table-box">
                        <table>
                            <thead>
                                <tr>
                                    <th>编号</th>
                                    <th>姓名</th>
                                    <th>性别</th>
                                    <th>出生年月</th>
                                    <th>供应商</th>
                                    <th>面试企业</th>
                                    <th>面试日期</th>
                                    <th>当天费用</th>
                                    <th>操作栏</th>
                                </tr>
                            </thead>
                            <tbody>
                                <asp:Repeater ID="Repeater_PersonnelRosterList" OnItemDataBound="Repeater_PersonnelRosterList_ItemDataBound" OnItemCommand="Repeater_PersonnelRosterList_ItemCommand" runat="server">
                                    <ItemTemplate>
                                        <tr style=‘background-color: <%#(Container.ItemIndex%2==0)?"#fff":"#eff6fa"%>‘>
                                            <td><%# Eval("Id") %></td>
                                            <td><%# Eval("Name") %></td>
                                            <td><%# Eval("Sex") %></td>
                                            <td><%# Eval("BrothDate") %></td>
                                            <td><%# Eval("GongYingShangName") %></td>
                                            <td><%# Eval("EnterpriseName") %></td>
                                            <td>
                                               <%# DateTime.Parse( Eval("MianShiDate").ToString()).ToShortDateString() %>
                                            </td>
                                            <td><%# Eval("TodayFeiYong") %></td>
                                            <td>
                                                <asp:HyperLink ID="BtnEditPersonnel" Visible="false" ToolTip="编辑详情、价格调整" runat="server"><img src="../images/ico/file_edit.gif" style="float:left;margin:8px;" /></asp:HyperLink>
                                                <asp:LinkButton ID="BtnDeletePersonnel" Visible="false" runat="server"  CommandName="DeletePersonnel" CommandArgument=‘<%# Eval("Id") %>‘ ToolTip="删除" ><img src="../images/ico/trash.gif" style="float:left;margin:8px;" /></asp:LinkButton>
                                            </td>
                                        </tr>
                                    </ItemTemplate>
                                </asp:Repeater>
                            </tbody>
                        </table>
                    </div>
                </div><!--表格结束-->
            <div class="announcement_splitpage">
                <div class="table_splitpage_div2">
                    <asp:LinkButton ID="LinkBtnToPage" OnClick="LinkBtnToPage_Click" CssClass="gotoPagebtn" runat="server"><div class="fenyebtn2">跳转</div></asp:LinkButton>
                    <span> <asp:TextBox ID="txtPageIndex" CssClass="toPageIndex" runat="server"></asp:TextBox>&nbsp;</span>
                    <asp:HyperLink ID="lnkNext" runat="server"><div class="fenyebtn"><img src="../images/ico/arrow_large_right.png" /></div></asp:HyperLink>&nbsp;
                   <span class="splitpagecount"><asp:Label ID="lbCountPage" runat="server" Text=""></asp:Label></span>
                     <span class="splitpagecount">/</span>
                     <span class="splitpagecount"><asp:Label ID="lbCurentPage" runat="server" Text=""></asp:Label></span>
                    <asp:HyperLink ID="lnkTop" runat="server"><div class="fenyebtn"><img src="../images/ico/arrow_large_left.png" /></div></asp:HyperLink>&nbsp;
                    <span class="splitpagecount">条数据</span>
                     <span class="splitpagecount"><asp:Label ID="lbDataCount" runat="server" Text=""></asp:Label></span>
                    <span class="splitpagecount">共计</span>
                </div>
            </div>

后台代码:

 PagedDataSource pds = new PagedDataSource();

        private int PageSize = 8;//定义分页每页初始大小

        StringBuilder sbUrl = new StringBuilder();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["gongyingshangName"] != null && Request.QueryString["startDate"] != null && Request.QueryString["endDate"] != null)
                {
                    this.txt_gongyingshangName.Value = Request.QueryString["gongyingshangName"].ToString();
                    this.txtMiana.Value = Request.QueryString["startDate"].ToString();
                    this.txtMianb.Value = Request.QueryString["endDate"].ToString();
                }
                else if (Request.QueryString["enterName"] != null && Request.QueryString["startDate"] != null && Request.QueryString["endDate"] != null)
                {
                    this.txt_enterName.Value = Request.QueryString["enterName"].ToString();
                    this.txtMiana.Value = Request.QueryString["startDate"].ToString();
                    this.txtMianb.Value = Request.QueryString["endDate"].ToString();
                }
                else if (Request.QueryString["employeName"] != null && Request.QueryString["startDate"] != null && Request.QueryString["endDate"] != null)
                {
                    this.txtName.Value = Request.QueryString["employeName"].ToString();
                    this.txtMiana.Value = Request.QueryString["startDate"].ToString();
                    this.txtMianb.Value = Request.QueryString["endDate"].ToString();
                }
                else if (Request.QueryString["startDate"] != null && Request.QueryString["endDate"] != null)
                {
                    this.txtMiana.Value = Request.QueryString["startDate"].ToString();
                    this.txtMianb.Value = Request.QueryString["endDate"].ToString();
                }
                BindPersonnelRosterList();
                this.DataBind();
            }
        }

        private void BindPersonnelRosterList()
        {
            LuYongRosterService lrs = new LuYongRosterService();

            List<LuYongMingDanInfo> lymdlist = new List<LuYongMingDanInfo>();

            if (this.txt_gongyingshangName.Value.ToString() != "供应商姓名")
            {
                if (String.IsNullOrWhiteSpace(this.txtMiana.Value.ToString().Trim()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘请选择面试开始日期!‘);", true);
                    this.txtMiana.Focus();
                    return;
                }
                if (String.IsNullOrWhiteSpace(this.txtMianb.Value.ToString().Trim()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘请选择面试结束日期!‘);", true);
                    this.txtMianb.Focus();
                    return;
                }
                if (DateTime.Parse(this.txtMiana.Value.ToString()) > DateTime.Parse(this.txtMianb.Value.ToString()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘面试开始日期不能大于面试结束日期!‘);", true);
                    this.txtMiana.Focus();
                    return;
                }
                if (DateTime.Parse(this.txtMiana.Value.ToString()) > DateTime.Parse(System.DateTime.Now.ToShortDateString()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘面试开始日期不能大于当天日期!‘);", true);
                    this.txtMiana.Focus();
                    return;
                }
                if (DateTime.Parse(this.txtMianb.Value.ToString()) > DateTime.Parse(System.DateTime.Now.ToShortDateString()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘面试结束日期不能大于当天日期!‘);", true);
                    this.txtMianb.Focus();
                    return;
                }

                sbUrl.Clear();
                sbUrl.Append("&gongyingshangName=" + this.txt_gongyingshangName.Value.ToString().Trim());
                sbUrl.Append("&startDate=" + this.txtMiana.Value.ToString().Trim());
                sbUrl.Append("&endDate=" + this.txtMianb.Value.ToString().Trim());
                lymdlist = lrs.GetAllLuYongRosterListByKeys(this.txt_gongyingshangName.Value.ToString().Trim(), null, null, this.txtMiana.Value.ToString().Trim(), this.txtMianb.Value.ToString().Trim());
            }
            else if (this.txt_enterName.Value.ToString() != "企业名称")
            {
                if (String.IsNullOrWhiteSpace(this.txtMiana.Value.ToString().Trim()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘请选择面试开始日期!‘);", true);
                    this.txtMiana.Focus();
                    return;
                }
                if (String.IsNullOrWhiteSpace(this.txtMianb.Value.ToString().Trim()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘请选择面试结束日期!‘);", true);
                    this.txtMianb.Focus();
                    return;
                }
                if (DateTime.Parse(this.txtMiana.Value.ToString()) > DateTime.Parse(this.txtMianb.Value.ToString()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘面试开始日期不能大于面试结束日期!‘);", true);
                    this.txtMiana.Focus();
                    return;
                }
                if (DateTime.Parse(this.txtMiana.Value.ToString()) > DateTime.Parse(System.DateTime.Now.ToShortDateString()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘面试开始日期不能大于当天日期!‘);", true);
                    this.txtMiana.Focus();
                    return;
                }
                if (DateTime.Parse(this.txtMianb.Value.ToString()) > DateTime.Parse(System.DateTime.Now.ToShortDateString()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘面试结束日期不能大于当天日期!‘);", true);
                    this.txtMianb.Focus();
                    return;
                }

                sbUrl.Clear();
                sbUrl.Append("&enterName=" + this.txt_enterName.Value.ToString().Trim());
                sbUrl.Append("&startDate=" + this.txtMiana.Value.ToString().Trim());
                sbUrl.Append("&endDate=" + this.txtMianb.Value.ToString().Trim());
                lymdlist = lrs.GetAllLuYongRosterListByKeys(null, this.txt_enterName.Value.ToString().Trim(), null, this.txtMiana.Value.ToString().Trim(), this.txtMianb.Value.ToString().Trim());
            }
            else if (this.txtName.Value.ToString() != "员工姓名")
            {
                if (String.IsNullOrWhiteSpace(this.txtMiana.Value.ToString().Trim()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘请选择面试开始日期!‘);", true);
                    this.txtMiana.Focus();
                    return;
                }
                if (String.IsNullOrWhiteSpace(this.txtMianb.Value.ToString().Trim()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘请选择面试结束日期!‘);", true);
                    this.txtMianb.Focus();
                    return;
                }
                if (DateTime.Parse(this.txtMiana.Value.ToString()) > DateTime.Parse(this.txtMianb.Value.ToString()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘面试开始日期不能大于面试结束日期!‘);", true);
                    this.txtMiana.Focus();
                    return;
                }
                if (DateTime.Parse(this.txtMiana.Value.ToString()) > DateTime.Parse(System.DateTime.Now.ToShortDateString()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘面试开始日期不能大于当天日期!‘);", true);
                    this.txtMiana.Focus();
                    return;
                }
                if (DateTime.Parse(this.txtMianb.Value.ToString()) > DateTime.Parse(System.DateTime.Now.ToShortDateString()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘面试结束日期不能大于当天日期!‘);", true);
                    this.txtMianb.Focus();
                    return;
                }

                sbUrl.Clear();
                sbUrl.Append("&employeName=" + this.txtName.Value.ToString().Trim());
                sbUrl.Append("&startDate=" + this.txtMiana.Value.ToString().Trim());
                sbUrl.Append("&endDate=" + this.txtMianb.Value.ToString().Trim());
                lymdlist = lrs.GetAllLuYongRosterListByKeys(null, null,this.txtName.Value.ToString().Trim(), this.txtMiana.Value.ToString().Trim(), this.txtMianb.Value.ToString().Trim());
            }
            else if (!String.IsNullOrWhiteSpace(this.txtMiana.Value.ToString().Trim()))
            {
                if (String.IsNullOrWhiteSpace(this.txtMianb.Value.ToString().Trim()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘请选择面试结束日期!‘);", true);
                    this.txtMianb.Focus();
                    return;
                }
                if (DateTime.Parse(this.txtMiana.Value.ToString()) > DateTime.Parse(this.txtMianb.Value.ToString()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘面试开始日期不能大于面试结束日期!‘);", true);
                    this.txtMiana.Focus();
                    return;
                }
                if (DateTime.Parse(this.txtMiana.Value.ToString()) > DateTime.Parse(System.DateTime.Now.ToShortDateString()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘面试开始日期不能大于当天日期!‘);", true);
                    this.txtMiana.Focus();
                    return;
                }
                if (DateTime.Parse(this.txtMianb.Value.ToString()) > DateTime.Parse(System.DateTime.Now.ToShortDateString()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘面试结束日期不能大于当天日期!‘);", true);
                    this.txtMianb.Focus();
                    return;
                }

                sbUrl.Clear();
                sbUrl.Append("&startDate=" + this.txtMiana.Value.ToString().Trim());
                sbUrl.Append("&endDate=" + this.txtMianb.Value.ToString().Trim());
                lymdlist = lrs.GetAllLuYongRosterListByKeys(null, null, null, this.txtMiana.Value.ToString().Trim(), this.txtMianb.Value.ToString().Trim());
            }
            else
            {
                lymdlist = lrs.GetAllLuYongRosterList();
            }

            pds.DataSource = lymdlist;
            pds.AllowPaging = true;

            pds.PageSize = PageSize;

            int CurrentPage;

            if (!String.IsNullOrWhiteSpace(this.txtPageIndex.Text.ToString().Trim()))
            {
                if (Helper.IsNum(this.txtPageIndex.Text.ToString().Trim()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(‘页码数只能输入数字!‘)", true);
                    this.txtPageIndex.Focus();
                    this.txtPageIndex.Text = this.lbCurentPage.Text.ToString();
                    return;
                }
                else if (int.Parse(this.txtPageIndex.Text.ToString().Trim()) > int.Parse(this.lbCountPage.Text.ToString().Trim()))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(‘所输页数不能大于总页数!‘)", true);
                    this.txtPageIndex.Focus();
                    this.txtPageIndex.Text = this.lbCountPage.Text.ToString();
                    return;
                }
                else
                {
                    CurrentPage = Convert.ToInt32(this.txtPageIndex.Text.ToString().Trim());
                }
            }
            else if (Request.QueryString["Page"] != null)
            {
                CurrentPage = Convert.ToInt32(Request.QueryString["Page"]);
            }
            else
            {
                CurrentPage = 1;
            }

            pds.CurrentPageIndex = CurrentPage - 1;//当前页的索引就等于当前页码-1;

            if (!pds.IsFirstPage)
            {
                //Request.CurrentExecutionFilePath 为当前请求的虚拟路径
                this.lnkTop.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage - 1) + sbUrl;
                //this.lnkFist.Enabled = this.lnkTop.Enabled = true;
                //this.lnkNext.Enabled = this.lnkLast.Enabled = true;
            }
            else
            {
                //this.lnkFist.Enabled = this.lnkTop.Enabled = false;
                //this.lnkNext.Enabled = this.lnkLast.Enabled = true;
                //this.lnkFist.Attributes.Add("style", "color:#ced9df;");
                this.lnkTop.Attributes.Add("style", "color:#ced9df;");
                this.lnkNext.Attributes.Remove("style");
                //this.lnkLast.Attributes.Remove("style");
            }
            if (!pds.IsLastPage)
            {
                //Request.CurrentExecutionFilePath 为当前请求的虚拟路径
                this.lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage + 1) + sbUrl;
                //this.lnkFist.Enabled = this.lnkTop.Enabled = true;
                //this.lnkNext.Enabled = this.lnkLast.Enabled = true;
            }
            else
            {
                //this.lnkNext.Enabled = this.lnkLast.Enabled = false;
                //this.lnkFist.Enabled = this.lnkTop.Enabled = true;
                this.lnkNext.Attributes.Add("style", "color:#ced9df;");
                //this.lnkLast.Attributes.Add("style", "color:#ced9df;");
                //this.lnkFist.Attributes.Remove("style");
                //this.lnkTop.Attributes.Remove("style");
            }
            //this.lnkFist.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);//跳转至首页
            //this.lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(pds.PageCount);//跳转至末页

            this.Repeater_PersonnelRosterList.DataSource = pds;
            this.Repeater_PersonnelRosterList.DataBind();
            this.lbCurentPage.Text = (pds.CurrentPageIndex + 1).ToString();
            this.lbCountPage.Text = pds.PageCount.ToString();
            this.lbDataCount.Text = lymdlist.Count.ToString();
            this.lbDataCount.Attributes.Add("style", "color:red;");
            this.txtPageIndex.Text = this.lbCurentPage.Text.ToString();
            this.BtnExportPersonnel.Attributes.Add("onclick", "return confirm(‘您确定要导出" + lymdlist.Count.ToString() + "条数据到表格?‘)");

            //判断用户是否有导出数据功能
            if (Session["MasterInfo"] != null)
            {
                MasterInfo masterInfo = Session["MasterInfo"] as MasterInfo;

                MasterRoleMenuButtonService mrmbs = new MasterRoleMenuButtonService();
                MasterRoleMenuButtonInfo mrmbInfo = mrmbs.GetMasterRoleMenuButtonInfo(masterInfo.RoleID.ToString(), BtnExportPersonnel.ID.ToString());
                if (mrmbInfo != null)
                {
                    BtnExportPersonnel.Visible = true;
                }
            }
            else
            {
                Response.Write("<script language=JavaScript>alert(‘登录超时!请重新登录‘);parent.location.href=‘/login.html‘;</script>");
                Response.End();
            }

        }
        /// <summary>
        /// 输入页码提交跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinkBtnToPage_Click(object sender, EventArgs e)
        {

            if (String.IsNullOrWhiteSpace(this.txtPageIndex.Text.ToString().Trim()))
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(‘页码不能为空!‘)", true);
                this.txtPageIndex.Focus();
                return;
            }
            else
            {
                BindPersonnelRosterList();
            }
        }
        /// <summary>
        /// 导出数据到Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinkBtnToExcel_Click(object sender, EventArgs e)
        {

            PageSize = 0;

            BindPersonnelRosterList();

            Export("application/ms-excel", "员工数据.xls");
        }
        /// <summary>
        /// 导出数据函数
        /// </summary>
        /// <param name="FileType">导出文件MIME类型</param>
        /// <param name="FileName">导出文件的名称</param>
        private void Export(String FileType, String FileName)
        {

            Response.Clear();
            Response.BufferOutput = true;
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            this.EnableViewState = false;
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            Repeater_PersonnelRosterList.RenderControl(hw);
            string str = hw.InnerWriter.ToString();
            Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">");
            Response.Write("</head><body><table>");
            Response.Write(sw.ToString());
            Response.Write("</table></body></html>");
            Response.End();

        }
        /// <summary>
        /// 如果需要实现导出Excel功能,则该函数需要重载,解释:确认在运行时为指定的ASP.NET 服务器控件呈现 HtmlForm 控件。
        /// </summary>
        /// <param name="control"></param>
        public override void VerifyRenderingInServerForm(Control control) { }

        protected void LinkBtnSearch_Click(object sender, EventArgs e)
        {
            //恢复分页初始页为第一页
            this.txtPageIndex.Text = "1";
            BindPersonnelRosterList();
        }

        protected void Repeater_PersonnelRosterList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DeletePersonnel")
            {
                string personId = e.CommandArgument.ToString();

                LuYongRosterService lyrs = new LuYongRosterService();

                int num = lyrs.DeleteLuYongMingDanById(int.Parse(personId));

                if (num > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘删除成功!‘);location=‘PersonnelManagement.aspx‘;", true);
                    return;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, GetType(), "", "alert(‘删除失败!‘);location=‘PersonnelManagement.aspx‘;", true);
                    return;
                }
            }
        }

        protected void Repeater_PersonnelRosterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (Session["MasterInfo"] != null)
            {
                MasterInfo masterInfo = Session["MasterInfo"] as MasterInfo;

                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    LuYongMingDanInfo tmskInfo = e.Item.DataItem as LuYongMingDanInfo;
                      //<a onclick="EditYuanGongState(<%# Eval("Id") %>);" title="编辑详情、价格调整" ></a>&nbsp;
                    LinkButton BtnDeletePersonnel = e.Item.FindControl("BtnDeletePersonnel") as LinkButton;
                    BtnDeletePersonnel.Attributes.Add("onclick", "return confirm(‘您确定要删除编号为:" + tmskInfo.Id.ToString() + "的人员信息?删除后无法恢复?‘)");

                    HyperLink BtnEditPersonnel = e.Item.FindControl("BtnEditPersonnel") as HyperLink;
                    BtnEditPersonnel.Attributes.Add("onclick", "EditYuanGongState(‘" + tmskInfo.Id.ToString() + "‘)");

                    MasterRoleMenuButtonService mrmbs = new MasterRoleMenuButtonService();
                    MasterRoleMenuButtonInfo mrmbInfo = mrmbs.GetMasterRoleMenuButtonInfo(masterInfo.RoleID.ToString(), BtnDeletePersonnel.ID.ToString());
                    if (mrmbInfo != null)
                    {
                        BtnDeletePersonnel.Visible = true;
                    }

                    MasterRoleMenuButtonInfo mrmbInfo2 = mrmbs.GetMasterRoleMenuButtonInfo(masterInfo.RoleID.ToString(), BtnEditPersonnel.ID.ToString());
                    if (mrmbInfo2 != null)
                    {
                        BtnEditPersonnel.Visible = true;
                    }
                }
            }
            else
            {
                Response.Write("<script language=JavaScript>alert(‘登录超时!请重新登录‘);parent.location.href=‘/login.html‘;</script>");
                Response.End();
            }

        }

代码解说:

private int PageSize = 8;//定义分页每页初始大小

之所以把pageSize定义为全局变量,并设置了固定的每页显示的条数,导出数据的时候,要把分页

PageSize = 0;然后再导出,要不然只能导出当前分页的第一个页面

StringBuilder sbUrl = new StringBuilder();

这个定义了一个可变字符串,要用它来储存筛选参数,

首先要判断this.txt_enterName.Value.ToString().Trim()这个文本框的值不为空或者非等于Null,将设置一个参数,把它写进sburl这个可变字符串中,多参数以此类推。。

sbUrl.Clear();
sbUrl.Append("&enterName=" + this.txt_enterName.Value.ToString().Trim());
sbUrl.Append("&startDate=" + this.txtMiana.Value.ToString().Trim());
sbUrl.Append("&endDate=" + this.txtMianb.Value.ToString().Trim());
lymdlist = lrs.GetAllLuYongRosterListByKeys(null, this.txt_enterName.Value.ToString().Trim(), null, this.txtMiana.Value.ToString().Trim(), this.txtMianb.Value.ToString().Trim());

下面这句话就是把sbUrl这个字符串拼接到url分页中,放到最后即可

if (!pds.IsFirstPage)
{
//Request.CurrentExecutionFilePath 为当前请求的虚拟路径
this.lnkTop.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage - 1) + sbUrl;
//this.lnkFist.Enabled = this.lnkTop.Enabled = true;
//this.lnkNext.Enabled = this.lnkLast.Enabled = true;
}

然后在页面加载的时候进行判断,如果url中的参数不为空,就讲对应的参数赋值到对应的文本框中,这样就实现了url分页功能

if (Request.QueryString["gongyingshangName"] != null && Request.QueryString["startDate"] != null && Request.QueryString["endDate"] != null)
{
this.txt_gongyingshangName.Value = Request.QueryString["gongyingshangName"].ToString();
this.txtMiana.Value = Request.QueryString["startDate"].ToString();
this.txtMianb.Value = Request.QueryString["endDate"].ToString();
}

根据用户提供的索引值进行翻页,其实也很简单

/// <summary>
/// 输入页码提交跳转
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkBtnToPage_Click(object sender, EventArgs e)
{

if (String.IsNullOrWhiteSpace(this.txtPageIndex.Text.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(‘页码不能为空!‘)", true);
this.txtPageIndex.Focus();
return;
}
else
{
BindPersonnelRosterList();//又重新绑定了数据源
}
}

BindPersonnelRosterList();中有这一段:

int CurrentPage;

if (!String.IsNullOrWhiteSpace(this.txtPageIndex.Text.ToString().Trim()))
{
if (Helper.IsNum(this.txtPageIndex.Text.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(‘页码数只能输入数字!‘)", true);
this.txtPageIndex.Focus();
this.txtPageIndex.Text = this.lbCurentPage.Text.ToString();
return;
}
else if (int.Parse(this.txtPageIndex.Text.ToString().Trim()) > int.Parse(this.lbCountPage.Text.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert(‘所输页数不能大于总页数!‘)", true);
this.txtPageIndex.Focus();
this.txtPageIndex.Text = this.lbCountPage.Text.ToString();
return;
}
else
{
CurrentPage = Convert.ToInt32(this.txtPageIndex.Text.ToString().Trim());
}
}
else if (Request.QueryString["Page"] != null)
{
CurrentPage = Convert.ToInt32(Request.QueryString["Page"]);
}
else
{
CurrentPage = 1;
}

这样就实现了跳转,

注意在查询的时候,

protected void LinkBtnSearch_Click(object sender, EventArgs e)
{
//恢复分页初始页为第一页,这个一定要加,要不然,如果你翻页到第10页的时候,想重新查询数据,虽然查到数据,万一查到的数据没有10页怎么办,也得索引会停留在10上面
this.txtPageIndex.Text = "1";
BindPersonnelRosterList();
}

导出表格其实和简单就是下面这三个方法:

/// <summary>
        /// 导出数据到Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinkBtnToExcel_Click(object sender, EventArgs e)
        {

            PageSize = 0;

            BindPersonnelRosterList();

            Export("application/ms-excel", "员工数据.xls");
        }
        /// <summary>
        /// 导出数据函数
        /// </summary>
        /// <param name="FileType">导出文件MIME类型</param>
        /// <param name="FileName">导出文件的名称</param>
        private void Export(String FileType, String FileName)
        {

            Response.Clear();
            Response.BufferOutput = true;
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            this.EnableViewState = false;
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            Repeater_PersonnelRosterList.RenderControl(hw);
            string str = hw.InnerWriter.ToString();
            Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">");
            Response.Write("</head><body><table>");
            Response.Write(sw.ToString());
            Response.Write("</table></body></html>");
            Response.End();

        }
        /// <summary>
        /// 如果需要实现导出Excel功能,则该函数需要重载,解释:确认在运行时为指定的ASP.NET 服务器控件呈现 HtmlForm 控件。
        /// </summary>
        /// <param name="control"></param>
        public override void VerifyRenderingInServerForm(Control control) { }
时间: 2024-10-24 20:28:40

repeater控件自定义Url分页带参数的相关文章

ASP.NET Repeater控件实现简单分页

早上,有看MSDN,看到了 PagedDataSource 类 http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.pageddatasource(v=vs.110).aspx 想起以前Insus.NET只对Gridview和DataList控件进行自定义分页.<GridView和DataList分页组件与用户控件>http://www.cnblogs.com/insus/archive/2009/03/19/14

repeater控件实现分页

repeater控件实现排序的方法,今天我再向大家介绍repeater控件如何实现分页的效果. 分页分为真分页和假分页. 真分页:控件上一页需要显示多少数据,就从数据库取出并绑定多少数据,每次换页时都需要访问数据库. 假分页:从数据库一次性取出所有数据绑定到控件上,再将所有数据根据一页显示多少条而分页. 从以上二者的概念上我们可以看出,区别在于分页时从数据库读取信息的方式,真分页的效率无疑是最高的.假分页在首次页面加载的时候会比较慢(如果数据量较多). 二者其实各有各的优缺点,可根据需要来自行选

Repeater控件-实现分页(升级版)

原来已经写了一个repeater控件的分页,今天有些了一个优化的程序. 1.解决了当数据条数,小于要显示的数据条数时,出现上一页按钮和还能继续递减的问题. 2.同时,还添加了在显示数据的表中进行删除和修改数据. 3.还解决了页面间的传值问题,(当点击Edit按钮时,页面跳转到另一个界面,也就是修改信息界面,然后进行修改,修改完成后保存并返回主界面.) 以上就是,新增的内容. 下面是代码: 总计使用了两个页面,一个是显示数据的页面(Default.aspx)另一个是修改信息的页面(Edit.asp

Repeater控件的分页实现

本文讲解Repeater控件与PagedDataSource相结合实现其分页功能.PagedDataSource 类封装那些允许数据源控件(如 DataGrid.GridView)执行分页操作的属性.如果控件开发人员需对自定义数据绑定控件提供分页支持,即可使用此类. PagedDataSource 类的部分公共属性: AllowCustomPaging // 获取或设置指示是否启用自定义分页的值. AllowPaging // 获取或设置指示是否启用分页的值. Count // 获取要从数据源使

Repeater控件中触发按钮事件出现System.ArgumentException: 回发或回调参数无效。在配置中使用 &lt; pages enableEventValidation=&quot;true&quot;/&gt;

今天,在Repeater控件中添加了一个按钮,可是在执行时确提示 System.ArgumentException: 回发或回调参数无效.在配置中使用 < pages enableEventValidation="true"/> <asp:Repeater ID="rpList" runat="server" OnItemCommand="rpList_ItemCommand"> <ItemTem

使用Sql分页方法给Repeater控件分页的方法

页面代码 <div class="bookList"> <asp:Repeater ID="rpBooks" runat="server"> <HeaderTemplate> <ul class="title"> <li class="bName">书名</li> <li class="bAuthor">

Repeater控件使用(含删除,分页功能)

Repeater控件使用(含删除,分页功能) 摘自:http://www.cnblogs.com/alanliu/archive/2008/02/25/914779.html 前臺代碼 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Repeater.aspx.cs" Inherits="Repeater" %> <!DOCTYPE html P

android控件---自定义带文本的ImageButton

由于SDK提供的ImageButton只能添加图片,不能添加文字:而Button控件添加的文字只能显示在图片内部:当我们需要添加文字在图片外部时就不能满足我们的需求了,顾只能自己写个自定义ImageButton.说是ImageButton,其实并不是继承于ImageButton,而是从LinearLayout继承,由于LinearLayout是线性排列,通过setOrientation(LinearLayout.VERTICAL)的方式达到View垂直排列的目的,所以很简单,只需要添加两个Vie

Repeater控件-2

本文转自:http://www.cnblogs.com/foolin/archive/2011/08/31/2161342.html 文章最后更新于2011年9月2日,增加Demo代码. 一般开发绑定数据的时候,Repeater控件是非常好用的控件,但是很多.net开发人员苦恼于无法分页,于是开发一个基于Repeater的分页控件,现在拿出来和大家一起分享分享.写得不好大家可以尽管拍砖,源码在本文后面.其实这个分页控件非常简单,分页控件只是根据记录数和当前页数等设置而输出一个分页导航条而已,并没