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> </span> <asp:HyperLink ID="lnkNext" runat="server"><div class="fenyebtn"><img src="../images/ico/arrow_large_right.png" /></div></asp:HyperLink> <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> <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> 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