自定义分页控件

以前都是用别人的分页控件,总想了解里控件里是什么内容。于是花了一个周末做了一个,虽然不太美观,但可以通过加css样式来控制一下就可以了。

这是第一次开发自定义控件,发出来,做个记录,同时也希望和正在做自定义控件的同志们共同交流

等以后加了新功能,再贴出来,呵呵。。

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Controls
{
    public class Pager : WebControl, IPostBackEventHandler
    {
        //总记录数
        public int RecordCount
        {
            get
            {
                if (ViewState["RecordCount"] != null)
                    return Convert.ToInt32(ViewState["RecordCount"]);
                return 0;
            }
            set
            {
                ViewState["RecordCount"] = value;
            }
        }
        //当前分页索引
        public int PageIndex
        {
            get
            {
                if (ViewState["PageIndex"] != null)
                    return Convert.ToInt32(ViewState["PageIndex"]);
                return 0;
            }
            set
            {
                ViewState["PageIndex"] = value;
            }
        }
        //分页大小
        public int PageSize
        {
            get
            {
                if (ViewState["PageSize"] != null)
                    return Convert.ToInt32(ViewState["PageSize"]);
                return 10;
            }
            set
            {
                ViewState["PageSize"] = value;
            }
        }
        #region 事件回传
        static object _PageIndexChanging = new object();

        public event EventHandler<NumericaArgs> PageIndexChanging
        {
            add
            {
                Events.AddHandler(_PageIndexChanging, value);
            }
            remove
            {
                Events.RemoveHandler(_PageIndexChanging, value);
            }
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            //控件自身的事件处理逻辑
            NumericaArgs args = new NumericaArgs(eventArgument);
            //触发用户注册的事件处理逻辑
            if (Events[_PageIndexChanging] != null)
            {
                (Events[_PageIndexChanging] as EventHandler<NumericaArgs>)(null, args);
            }
        }
        public class NumericaArgs : EventArgs
        {
            private int _newPageIndex;

            public int NewPageIndex
            {
                get
                {
                    return _newPageIndex;
                }
                set
                {
                    _newPageIndex = value;
                }
            }
            public NumericaArgs(string args)
            {
                _newPageIndex = Convert.ToInt32(args);
            }
        }
        #endregion

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            int pageCount = 0;
            if ((RecordCount % PageSize) > 0)
                pageCount = RecordCount / PageSize+1;
            else
                pageCount = RecordCount / PageSize;


            //base.RenderContents(writer);
            //first
            string firstRef = Page.ClientScript.GetPostBackClientHyperlink(this, "0");//向上加一页

            //设置可用状态
            if (PageIndex <= 0)
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
            else
                writer.AddAttribute(HtmlTextWriterAttribute.Href, firstRef);

            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write("首页");
            writer.RenderEndTag();
            //prev
            string prevRef = Page.ClientScript.GetPostBackClientHyperlink(this, (PageIndex - 1).ToString());//向上加一页
            if (PageIndex <= 0)
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
            else
                writer.AddAttribute(HtmlTextWriterAttribute.Href, prevRef);

            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write("上一页");
            writer.RenderEndTag();
            //next
            string nextRef = Page.ClientScript.GetPostBackClientHyperlink(this, (PageIndex + 1).ToString());//向上加一页

            if (PageIndex >= pageCount - 1)
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
            else
                writer.AddAttribute(HtmlTextWriterAttribute.Href, nextRef);

            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write("下一页");
            writer.RenderEndTag();
            //last
            string lastRef = Page.ClientScript.GetPostBackClientHyperlink(this, (pageCount-1).ToString());//向上加一页

            if (PageIndex >= pageCount - 1)
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
            else
                writer.AddAttribute(HtmlTextWriterAttribute.Href, lastRef);

            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write("末页");
            writer.RenderEndTag();

            //添加dropdownlist
            DropDownList ddl = new DropDownList();
            ddl.AutoPostBack = true;
            ddl.ID = "ddlPage";
            for (int i = 0; i < pageCount; i++)
            {
                string page = (i + 1).ToString();
                ddl.Items.Add(new ListItem(page,i.ToString()));
            }
            //添加事件返回
            ddl.Attributes.Add("onchange", "javascript:__doPostBack(\'" + this.UniqueID + "\',this.value)");
            ddl.SelectedValue = PageIndex.ToString();
            ddl.RenderControl(writer);
            writer.Write(string.Format(" 第{0}页 ", (PageIndex + 1)));
            writer.Write(string.Format(" 共{0}页 ",pageCount));
           
        }
        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.RenderEndTag();
        }
    }
}

编译以后在工具箱中找到此控件,然后抓到页面上就可以用了,

在页面上写两个方法一个是gridview的绑定,另一个是pager的PageIndexChanging 

public void GvBind()
        {
            Pager1.PageSize = 10;
            Pager1.RecordCount = Model.Get_ModelCount();//赋值记录数s
            this.GridView1.DataKeyNames = new string[] { "ids" };
            this.GridView1.DataSource = Model.Get_ModelByPage(Pager1.PageIndex, Pager1.PageSize);//分页获取记录
            this.GridView1.DataBind();
        }

 protected void Pager1_PageIndexChanging(object sender, Controls.Pager.NumericaArgs e)
        {
            Pager1.PageIndex = e.NewPageIndex;
            GvBind();
        }

你可能感兴趣的:(自定义)