用户自定义分页——PageNavigato

       在程序当中遇到了一个分页功能,这个看起来挺简单的,但是在这个程序中他是由自己写的。对于分页我们并不陌生,当初在学习牛腩新闻发布系统的时候,到了最后牛腩老师给我们留了一个可以算是作业——分页。分页当时他提出来的应该是使用空间来进行分页,当然在后边我们学习的ASP.Net的时候也有降到分页技术。 

        在这里就和大家分享一下用户自定义的分页是如何使用的。比如在ProductList页面中使用到了此控件。


一、用户自定义控件的代码:

前台:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PageNavigator.ascx.cs" Inherits="YXShopAdmin.Admin.Control.PageNavigator" %>
共<asp:Label runat="server" ID="LblRecordCount"/>条记录,共<asp:Label runat="server" ID="LblPageCount"/>页,当前第<asp:Label runat="server" ID="LblPageIndex" />页
 <asp:LinkButton ID="LnkBtnFirst" runat="server" CommandName="Page" OnClick="LnkBtnFirst_Click" CssClass="list_link">首页</asp:LinkButton> 
 
 <asp:LinkButton ID="LnkBtnPrevious" runat="server" CommandName="Page" OnClick="LnkBtnPrevious_Click" CssClass="list_link">上一页</asp:LinkButton> 

 <asp:LinkButton ID="LnkBtnNext" runat="server" CommandName="Page" OnClick="LnkBtnNext_Click" CssClass="list_link">下一页</asp:LinkButton> 
 
 <asp:LinkButton ID="LnkBtnLast" runat="server" CommandName="Page" OnClick="LnkBtnLast_Click" CssClass="list_link">尾页</asp:LinkButton> 
<asp:textbox id="txtNewPageIndex" runat="server" width="20px" CssClass="form"/>
<asp:linkbutton id="LnkBtnGoto" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="转到此页" OnClick="LnkBtnGoto_Click" CssClass="list_link" />

后台:

 public delegate void PageChangeHandler(object send, int nPageIndex);
    public partial class PageNavigator : System.Web.UI.UserControl
    {
        public event PageChangeHandler OnPageChange;
        private int _PageCount = 0;
        private int _RecordCount = 0;
        private int _PageIndex = 1;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public int PageCount
        {
            get
            {
                return int.Parse(this.LblPageCount.Text);
            }
            set
            {
                _PageCount = value;
                /*
                if (_PageCount < 2)
                {
                    //this.txtNewPageIndex.Enabled = false;
                    //this.LnkBtnGoto.Enabled = false;
                }
                else
                {
                    //this.txtNewPageIndex.Enabled = true;
                    //this.LnkBtnGoto.Enabled = true;
                }
                 */
                this.LblPageCount.Text = _PageCount.ToString();
            }
        }
        public int RecordCount
        {
            get
            {
                return int.Parse(this.LblRecordCount.Text);
            }
            set
            {
                _RecordCount = value;
                this.LblRecordCount.Text = _RecordCount.ToString();
            }
        }
        public int PageIndex
        {
            get
            {
                return int.Parse(this.LblPageIndex.Text);
            }
            set
            {
                _PageIndex = value;
                this.txtNewPageIndex.Text = this.LblPageIndex.Text = _PageIndex.ToString();
                if (_PageIndex < 2)
                {
                    this.LnkBtnFirst.Enabled = false;
                    this.LnkBtnPrevious.Enabled = false;
                }
                else
                {
                    this.LnkBtnFirst.Enabled = true;
                    this.LnkBtnPrevious.Enabled = true;
                }
                if (_PageIndex >= _PageCount)
                {
                    this.LnkBtnNext.Enabled = false;
                    this.LnkBtnLast.Enabled = false;
                }
                else
                {
                    this.LnkBtnNext.Enabled = true;
                    this.LnkBtnLast.Enabled = true;
                }
            }
        }

        protected void LnkBtnFirst_Click(object sender, EventArgs e)
        {
            try
            {
                OnPageChange(sender, 1);
            }
            catch { }
        }
        protected void LnkBtnPrevious_Click(object sender, EventArgs e)
        {
            try
            {
                int n = Convert.ToInt32(this.LblPageIndex.Text);
                n--;
                OnPageChange(sender, n);
            }
            catch
            {
                throw;
            }
        }
        protected void LnkBtnNext_Click(object sender, EventArgs e)
        {
          
                int n = Convert.ToInt32(this.LblPageIndex.Text);
                n++;
                OnPageChange(sender, n);
            
        }
        protected void LnkBtnLast_Click(object sender, EventArgs e)
        {
            try
            {
                int n = Convert.ToInt32(this.LblPageCount.Text);
                OnPageChange(sender, n);
            }
            catch
            {
                throw;
            }
        }
        protected void LnkBtnGoto_Click(object sender, EventArgs e)
        {
            int n;
            try
            {
                n = Convert.ToInt32(this.txtNewPageIndex.Text);
            }
            catch
            {
                n = Convert.ToInt32(this.LblPageIndex.Text);
            }
            int np = Convert.ToInt32(this.LblPageCount.Text.Trim());
            if (n > np)
                n = np;
            if (n < 1)
                n = 1;
            OnPageChange(sender, n);
        }
    }


二、前台(productList)使用:

引入控件:

<%@ Register Src="~/Saler/Admin/Control/PageNavigator.ascx" TagName="PageNavigator" TagPrefix="uc1" %>


调用:

<td><uc1:PageNavigator ID="PageNavigator1" runat="server" /></td>

后台控制:

protected void Page_Load(object sender, EventArgs e)
 {
    this.PageNavigator1.OnPageChange += new PageChangeHandler(PageNavigator1_PageChange);
}
 
 //获得列表
        protected void PageNavigator1_PageChange(object sender, int PageIndex)
        {
            StartLoad(PageIndex);
        }

        protected void StartLoad(int PageIndex)
        {
            int i = 0;
            int j = 0;
            //因为登陆页面上的需要MemberID,但是自己的登陆页面没有写,所以这里就处错误了
            if (Session["MemberID"].ToString() == null)
            {
                Response.Redirect("../../Login.aspx");
                return;
            }
            List<YXShop.Model.Product> data = BLL.GetListByColumn("*", "Pro_PutoutID=" + Session["MemberID"] + " and Pro_PutoutTypeId=1 ", PageIndex, 15, out i, out j);
            this.PageNavigator1.PageCount = j;
            this.PageNavigator1.PageIndex = PageIndex;
            this.PageNavigator1.RecordCount = i;
            Productlists.DataSource = data;                              //设置datalist数据源
            Productlists.DataBind();                                   //绑定数据源
        }

</pre><pre>

三、效果:



       以上的功能都可以实现。

      现在把这种方法分享给大家的意图:这种做法维护起来好维护,扩展性好。实现起来也不麻烦。希望对大家有所帮助。

     学习ing......

你可能感兴趣的:(用户自定义分页——PageNavigato)