zoukankan      html  css  js  c++  java
  • 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;每页<span
                    class="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 (e.CommandArgument.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}
    {1}", condition.Substring(0, condition.LastIndexOf("
    ")), 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;
    }
  • 相关阅读:
    NLPIR大数据语义系统文本数据分析挖掘平台
    NLPIR:中文语义挖掘是自然语言处理的关键
    NLPIR-JZSearch智能搜索深层挖掘大数据资源
    NLPIR智能Protege知识图谱实现多场景应用
    DotNet加密方式解析--散列加密
    SSO单点登录PHP简单版
    xss框架基础框架实现
    Kafka Offset Storage
    C#如何使用ES
    模拟Vue之数据驱动
  • 原文地址:https://www.cnblogs.com/ahhswyf/p/3459748.html
Copyright © 2011-2022 走看看