300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > ASP.NET用户自定义分页控件

ASP.NET用户自定义分页控件

时间:2020-08-02 06:50:12

相关推荐

ASP.NET用户自定义分页控件

1、新建Web用户控件,命名为“wucPager.ascx”。

在.ascx中加入代码:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="wucPager.ascx.cs" Inherits="Web.webcontrols.wucPager" %><table width="100%" border="0" cellspacing="0" cellpadding="0" class="total"><tr><td align="left">共<span class="number"><asp:Literal ID="ltlRecordCount" runat="server" Text="0"></asp:Literal></span>记录&nbsp;每页<spanclass="number"><asp:Literal ID="ltlPageSize" runat="server" Text="10"></asp:Literal></span>条共<span class="number"><asp:Literal ID="ltlPageCount" runat="server"></asp:Literal></span>页</td></tr></table><table width="100%" border="0" cellspacing="0" cellpadding="0" class="page"><tr><td>&nbsp;</td><td width="75"><div class="buttons"><asp:LinkButton ID="lbtnFirst" runat="server" CssClass="regular" OnCommand="ToggleCommon_Click"CommandArgument="first"> <img src="../images/page-first.png" alt="" />首页</asp:LinkButton></div></td><td width="85"><div class="buttons"><asp:LinkButton ID="lbtnPrevious" runat="server" CssClass="regular" OnCommand="ToggleCommon_Click"CommandArgument="previous"><img src="../images/page-prev.png" alt="" />上一页</asp:LinkButton></div></td><td width="85"><div class="buttons"><asp:LinkButton ID="lbtnNext" runat="server" CssClass="regular" OnCommand="ToggleCommon_Click"CommandArgument="next"><img src="../images/page-next.png" alt="" />下一页</asp:LinkButton></div></td><td width="75"><div class="buttons"><asp:LinkButton ID="lbtnLast" runat="server" CssClass="regular" OnCommand="ToggleCommon_Click"CommandArgument="last"> <img src="../images/page-last.png" alt="" />末页</asp:LinkButton></div></td><td width="60">跳转到:</td><td width="45"><asp:TextBox ID="txtCurrentPage" runat="server" MaxLength="6" Width="34px"></asp:TextBox></td><td width="35"><asp:ImageButton ID="ibtnGo" runat="server" ImageUrl="../images/go.gif" Width="14"Height="14" Enabled="false" CommandArgument="go" OnCommand="ToggleCommon_Click" /></td></tr></table><asp:RegularExpressionValidator ID="revCurrentPage" runat="server" ControlToValidate="txtCurrentPage"ValidationExpression="\d*" SetFocusOnError="true"></asp:RegularExpressionValidator><asp:RangeValidator ID="rvCurrentPage" runat="server" ControlToValidate="txtCurrentPage"SetFocusOnError="true" Type="Integer"></asp:RangeValidator>

2、在.ascx.cs中加入代码:

using System;using System.Web.UI.WebControls;namespace Web.webcontrols{public partial class wucPager : System.Web.UI.UserControl{#region 分页属性/// <summary>/// 记录总数/// </summary>public int RecordCount{get{if (ViewState["RecordCount"] == null){return 0;}return (int)ViewState["RecordCount"];}set{ViewState["RecordCount"] = value;}}/// <summary>/// 一页显示的记录数/// </summary>public int PageSize{get{if (ViewState["PageSize"] == null){return 10;}return (int)ViewState["PageSize"];}set{ViewState["PageSize"] = value;}}/// <summary>/// 页总数/// </summary>public int PageCount{get{int pageIndex = RecordCount / PageSize;return (RecordCount % PageSize == 0 ? pageIndex : pageIndex + 1);}}/// <summary>/// 页索引/// </summary>public int CurrentPageIndex{get{if (ViewState["CurrentPageIndex"] == null){return 1;}return (int)ViewState["CurrentPageIndex"];}set{ViewState["CurrentPageIndex"] = value;}}public event PageChangedEventHandler PageChanged;#endregion#region 页面加载protected void Page_PreRender(object sender, EventArgs e){LoadPageData();}#endregion#region 加载分页信息private void LoadPageData(){//一页显示的记录数ltlPageSize.Text = PageSize.ToString();//总页数ltlPageCount.Text = PageCount.ToString();//记录总数ltlRecordCount.Text = RecordCount.ToString();//记录数为0时,按钮全部禁用if (ltlPageCount.Text == "0"){txtCurrentPage.Text = "0";txtCurrentPage.Enabled = false;rvCurrentPage.MinimumValue = "0";rvCurrentPage.MaximumValue = "0";ibtnGo.Enabled = false;lbtnFirst.Enabled = false;lbtnPrevious.Enabled = false;lbtnNext.Enabled = false;lbtnLast.Enabled = false;}else{txtCurrentPage.Enabled = true;rvCurrentPage.MinimumValue = "1";rvCurrentPage.MaximumValue = ltlPageCount.Text;ibtnGo.Enabled = true;lbtnFirst.Enabled = true;//如果当前页数大于总页数,当前页数等于总页数if (CurrentPageIndex > PageCount){CurrentPageIndex = PageCount;FirePageChanged();return;}//设定上一页,下一页按钮启用状态if (CurrentPageIndex == 1){lbtnPrevious.Enabled = false;if (ltlPageCount.Text != "1"){lbtnNext.Enabled = true;}else{lbtnNext.Enabled = false;}}else if (CurrentPageIndex == PageCount){lbtnPrevious.Enabled = true;lbtnNext.Enabled = false;}else{lbtnPrevious.Enabled = true;lbtnNext.Enabled = true;}lbtnLast.Enabled = true;txtCurrentPage.Text = CurrentPageIndex.ToString();}}#endregion#region 共用按钮事件protected void ToggleCommon_Click(object sender, CommandEventArgs e){switch (mandArgument.ToString()){case "go":if (txtCurrentPage.Text == ""){txtCurrentPage.Text = "1";}CurrentPageIndex = int.Parse(txtCurrentPage.Text);break;case "first":CurrentPageIndex = 1;break;case "previous":CurrentPageIndex -= 1;break;case "next":CurrentPageIndex += 1;break;case "last":CurrentPageIndex = PageCount;break;}FirePageChanged();}private void FirePageChanged(){if (PageChanged != null){PageChanged(this, EventArgs.Empty);}if (Session["PageBack"] != null){string condition = Session["PageBack"].ToString();Session["PageBack"] = string.Format("{0}\r{1}", condition.Substring(0, condition.LastIndexOf("\r")), CurrentPageIndex);}}#endregion#region 自定义分页事件public delegate void PageChangedEventHandler(object sender, EventArgs args);#endregion}}

3、在.aspx中调用:

<wuc:Pager ID="Pager" runat="server" OnPageChanged="Pager_PageChanged" />

4、在.cs中数据绑定时为分页控件赋值:

protected void BindRptData(){AAAEntity model = new AAAEntity();int recordCount = -1;DataSet ds = GetPaged(Pager.PageSize, Pager.CurrentPageIndex, ref recordCount, model);rpList.DataSource = ds;Pager.RecordCount = recordCount;rpList.DataBind();}

5、在.cs中添加分页事件:

protected void Pager_PageChanged(object sender, EventArgs e){BindRptData();}

6、分页控件中用到的CSS样式:

.page{height: 34px;line-height: 34px;border-bottom: 1px solid #9DB3C5;border-right: 1px solid #9DB3C5;border-left: 1px solid #9DB3C5;font-size: 12px;color: #333;}.page tr{background: url(../images/title_bg2.jpg) top;}.total{border-bottom: 1px solid #9DB3C5;border-right: 1px solid #9DB3C5;border-left: 1px solid #9DB3C5;line-height: 38px;height: 38px;font-size: 12px;color: #434343;}.total td{padding-left: 5px;padding-right: 5px;}.number{font-size: 14px;color: #F30;font-weight: bold;text-decoration: underline;margin-left: 5px;margin-right: 5px;}

.buttons a, .buttons button{display: block;float: left;margin: 0 7px 0 0;background: url(../images/buttonbg.gif);border: 1px solid #c0c0c0;font-family: "Lucida Grande" , Tahoma, Arial, Verdana, sans-serif;font-size: 12px;line-height: 120%;text-decoration: none;color: #565656;cursor: pointer;padding: 5px 10px 6px 7px; /* Links */}.buttons button{width: auto;overflow: visible;padding: 4px 10px 3px 7px; /* IE6 */}.buttons button[type]{padding: 5px 10px 5px 7px; /* Firefox */line-height: 17px; /* Safari */}*:first-child + html button[type]{padding: 4px 10px 3px 7px; /* IE7 */}.buttons button img, .buttons a img{margin: 0 3px -3px 0 !important;padding: 0;border: none;width: 16px;height: 16px;}/* STANDARD */button:hover, .buttons a:hover{background: url(../images/buttonbg2.gif);border: 1px solid #c2e1ef;color: #336699;}.buttons a:active{background-color: #6299c5;border: 1px solid #6299c5;color: #fff;}/* REGULAR */button.regular, .buttons a.regular{color: #313131;}.buttons a.regular:hover, button.regular:hover{background-color: #dff4ff;border: 1px solid #f8b551;color: #ec6941;}.buttons a.regular:active{background-color: #6299c5;border: 1px solid #00b7ee;color: #00b7ee;}button.big, buttons a.big{color: #FC0;}.buttons a.big:hover, button.big:hover{background-color: #dff4ff;border: 1px solid #f8b551;color: #ec6941;}.buttons a.regular:active{background-color: #6299c5;border: 1px solid #00b7ee;color: #00b7ee;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。