ASP.NET控件开发之-------自定义分页控件

本分页控件共四个文件,代码全部折叠,请打开查看,代码如下:

1.events.cs

events.cs
 1 using System;

 2 using System.Collections.Generic;

 3 using System.Text;

 4 using System.Web.UI.WebControls;

 5 using System.Web.UI.HtmlControls;

 6 using System.Web.UI;

 7 using System.ComponentModel;

 8 

 9 

10 namespace Yangpu.Web.ExControls

11 {

12     /// <summary>

13     /// ASP.NET WebForm 分页控件

14     /// <remarks>

15     /// <author>作者:杨璞</author>

16     /// <CreateDataTime>创建日期:2012-09-26</CreateDataTime>

17     /// <LastModefyTime>修改日期:2012-09-26</LastModefyTime>

18     /// <WeiboAddress>新浪微博地址: http://weibo.com/yangpu1983</WeiboAddress>

19     /// </remarks>

20     /// </summary>

21     public partial class Pager:IPostBackDataHandler,IPostBackEventHandler

22     {

23         private readonly static object EventPageIndexChanged = new object();

24 

25         [Description("页序号改变事件\n作者:杨璞\n创建日期:2012-09-26\n修改日期:2012-09-26\n新浪微博地址: http://weibo.com/yangpu1983")]

26         /// <summary>

27         /// 页序号改变事件

28         /// <author>作者:杨璞</author>

29         /// <CreateDataTime>创建日期:2012-09-26</CreateDataTime>

30         /// <LastModefyTime>修改日期:2012-09-26</LastModefyTime>

31         /// <WeiboAddress>新浪微博地址: http://weibo.com/yangpu1983</WeiboAddress>

32         /// </summary>

33         public event PageIndexChangedEventHandler PageIndexChanged

34         {

35             add { Events.AddHandler(EventPageIndexChanged, value); }

36             remove { Events.RemoveHandler(EventPageIndexChanged, value); }

37         }

38 

39         protected virtual void OnPageIndexChanged(PageIndexChangedEventArgs e)

40         {

41             PageIndexChangedEventHandler handlers = (PageIndexChangedEventHandler)Events[EventPageIndexChanged];

42             if (handlers != null)

43             {

44                 handlers(this, e);

45             }

46         }

47 

48         public void RaisePostBackEvent(string eventArgument)

49         {

50             PageIndexChangedEventArgs e = new PageIndexChangedEventArgs();

51             switch (eventArgument)

52             {

53                 case "first": this.PageIndex = 0; break;

54                 case "previouse": this.PageIndex--; break;

55                 case "next": this.PageIndex++; break;

56                 case "last": this.PageIndex = this.PageCount - 1; break;

57                 case "SelectedIndexChanged": break;

58                 default: this.PageIndex = int.Parse(eventArgument); break;

59             }

60             e.PageIndex = this.PageIndex;

61             OnPageIndexChanged(e);

62         }

63 

64         public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)

65         {

66             string strOldValue = this.PageIndex.ToString();

67             string strNewValue = postCollection[this.UniqueID];

68             if (strOldValue == null || (strOldValue != null && !strOldValue.Equals(strNewValue)))

69             {

70                 this.PageIndex = int.Parse(strNewValue); return true;

71             }

72             return false;

73         }

74 

75         public void RaisePostDataChangedEvent()

76         {

77             PageIndexChangedEventArgs e = new PageIndexChangedEventArgs();

78             e.PageIndex = this.PageIndex;

79             OnPageIndexChanged(e);

80         }

81 

82     }

83 }

 

2.Pager.cs

Pager.cs
using System;

using System.Collections.Generic;

using System.Text;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Web.UI;

using System.ComponentModel;



namespace Yangpu.Web.ExControls

{



    [DefaultProperty("PageCount")]

    [DefaultEvent("PageIndexChanged")]

    [ParseChildren(false)]

    [PersistChildren(false)]

    [ToolboxData("<{0}:Pager runat=server></{0}:Pager>")]

    [Description("ASP.NET WebForm 分页控件\n作者:杨璞\n创建日期:2012-09-26\n修改日期:2012-09-26\n新浪微博地址: http://weibo.com/yangpu1983")]

    /// <summary>

    /// ASP.NET WebForm 分页控件

    /// <remarks>

    /// <author>作者:杨璞</author>

    /// <CreateDataTime>创建日期:2012-09-26</CreateDataTime>

    /// <LastModefyTime>修改日期:2012-09-26</LastModefyTime>

    /// <WeiboAddress>新浪微博地址: http://weibo.com/yangpu1983</WeiboAddress>

    /// </remarks>

    /// </summary>

    public partial class Pager:Control

    {



        protected override void Render(HtmlTextWriter writer)

        {

            base.Render(writer);



            switch (this.RenderStyle)

            {

                case RenderType.Link:

                    {



                        ////第一页

                        //writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink(this, "first"));

                        //writer.AddAttribute(HtmlTextWriterAttribute.Style, "padding:3px 3px;margin:3px;");

                        //if (this.PageIndex == 0)

                        //    writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "false");

                        //writer.RenderBeginTag(HtmlTextWriterTag.A);

                        //writer.Write("第一页");

                        //writer.RenderEndTag();



                        //上一页

                        writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink(this, "previouse"));

                        writer.AddAttribute(HtmlTextWriterAttribute.Style, "padding:3px 3px;margin:3px;font-size:14px;");

                        if (this.PageIndex == 0)

                            writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "false");

                        writer.RenderBeginTag(HtmlTextWriterTag.A);

                        writer.Write("上一页");

                        writer.RenderEndTag();





                        //链接

                        for (int i = 0; i < this.PageCount; i++)

                        {

                            writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink(this, i.ToString()));

                            writer.AddAttribute(HtmlTextWriterAttribute.Style, "padding:3px 3px;margin:3px;font-size:14px;");

                            if (i == this.PageIndex)

                                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "false");

                            writer.RenderBeginTag(HtmlTextWriterTag.A);

                            writer.Write(i + 1);

                            writer.RenderEndTag();

                        }





                        //下一页

                        writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink(this, "next"));

                        writer.AddAttribute(HtmlTextWriterAttribute.Style, "padding:3px 3px;margin:3px;font-size:14px;");

                        if (this.PageCount == 0 || this.PageIndex == this.PageCount - 1)

                            writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "false");

                        writer.RenderBeginTag(HtmlTextWriterTag.A);

                        writer.Write("下一页");

                        writer.RenderEndTag();



                        ////最末页

                        //writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink(this, "last"));

                        //writer.AddAttribute(HtmlTextWriterAttribute.Style, "padding:3px 3px;margin:3px;");

                        //if (this.PageCount == 0 || this.PageIndex == this.PageCount - 1)

                        //    writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "false");

                        //writer.RenderBeginTag(HtmlTextWriterTag.A);

                        //writer.Write("最末页");

                        //writer.RenderEndTag();

                    } break;

                case RenderType.Button:

                    {



                        writer.RenderBeginTag(HtmlTextWriterTag.Label);

                        writer.Write(string.Format("共 {3} 条记录,每页显示 {1} 条, 当前为 {0}/{2} 页", this.PageIndex + 1, this.PageSize, this.PageCount,this.RowCount));

                        writer.RenderEndTag();



                        //第一页

                        writer.AddAttribute(HtmlTextWriterAttribute.Value, "第一页");

                        writer.AddAttribute(HtmlTextWriterAttribute.Type, "Submit");

                        if (this.PageIndex == 0)

                            writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "false");

                        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackClientHyperlink(this, "first"));

                        writer.RenderBeginTag(HtmlTextWriterTag.Input);

                        writer.RenderEndTag();





                        //上一页

                        writer.AddAttribute(HtmlTextWriterAttribute.Value, "上一页");

                        writer.AddAttribute(HtmlTextWriterAttribute.Type, "Submit");

                        if (this.PageIndex == 0)

                            writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "false");

                        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackClientHyperlink(this, "previouse"));

                        writer.RenderBeginTag(HtmlTextWriterTag.Input);

                        writer.RenderEndTag();



                        //下一页

                        writer.AddAttribute(HtmlTextWriterAttribute.Value, "下一页");

                        writer.AddAttribute(HtmlTextWriterAttribute.Type, "Submit");

                        if (this.PageCount == 0 || this.PageIndex == this.PageCount - 1)

                            writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "false");

                        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackClientHyperlink(this, "next"));

                        writer.RenderBeginTag(HtmlTextWriterTag.Input);

                        writer.RenderEndTag();



                        //最末页

                        writer.AddAttribute(HtmlTextWriterAttribute.Value, "最末页");

                        writer.AddAttribute(HtmlTextWriterAttribute.Type, "Submit");

                        if (this.PageCount == 0 || this.PageIndex == this.PageCount - 1)

                            writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "false");

                        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackClientHyperlink(this, "last"));

                        writer.RenderBeginTag(HtmlTextWriterTag.Input);

                        writer.RenderEndTag();



                    } break;

                case RenderType.DropDownList:

                    {



                        writer.RenderBeginTag(HtmlTextWriterTag.Label);

                        writer.Write(string.Format("共 {3} 条记录,每页显示 {1} 条, 当前为 {0}/{2} 页", this.PageIndex + 1, this.PageSize, this.PageCount,this.RowCount));

                        writer.RenderEndTag();



                        // 下拉列表

                        writer.AddAttribute(HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackClientHyperlink(this, "SelectedIndexChanged"));

                        writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);

                        writer.RenderBeginTag(HtmlTextWriterTag.Select);

                        for (int i = 0; i < this.PageCount; i++)

                        {

                            writer.AddAttribute(HtmlTextWriterAttribute.Value, i.ToString());

                            if (i == this.PageIndex)

                                writer.AddAttribute(HtmlTextWriterAttribute.Selected, "true");

                            writer.RenderBeginTag(HtmlTextWriterTag.Option);

                            writer.Write(string.Format("第{0}页", i + 1));

                            writer.RenderEndTag();

                        }

                        writer.RenderEndTag();

                    } break;

            }

        }



    }

}

 

3.Properties.cs

Properties.cs
using System;

using System.Collections.Generic;

using System.Text;

using System.ComponentModel;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;



namespace Yangpu.Web.ExControls

{

    /// <summary>

    /// ASP.NET WebForm 分页控件

    /// <remarks>

    /// <author>作者:杨璞</author>

    /// <CreateDataTime>创建日期:2012-09-26</CreateDataTime>

    /// <LastModefyTime>修改日期:2012-09-26</LastModefyTime>

    /// <WeiboAddress>新浪微博地址: http://weibo.com/yangpu1983</WeiboAddress>

    /// </remarks>

    /// </summary>

    public partial class Pager

    {

        [Category("数据"), DefaultValue(0),Description("每页显示数据条数\n作者:杨璞\n创建日期:2012-09-26\n修改日期:2012-09-26\n新浪微博地址: http://weibo.com/yangpu1983")]

        /// <summary>

        /// 每页显示数据条数

        /// <author>作者:杨璞</author>

        /// <CreateDataTime>创建日期:2012-09-26</CreateDataTime>

        /// <LastModefyTime>修改日期:2012-09-26</LastModefyTime>

        /// <WeiboAddress>新浪微博地址: http://weibo.com/yangpu1983</WeiboAddress>

        /// </summary>

        public int PageSize

        {

            get

            {

                if (ViewState["PageSize"] == null)

                    ViewState["PageSize"] = 0;

                return Convert.ToInt32(ViewState["PageSize"]);

            }

            set

            {

                ViewState["PageSize"] = value;

            }

        }



        [Category("数据"), DefaultValue(0), Description("总记录条数\n作者:杨璞\n创建日期:2012-09-26\n修改日期:2012-09-26\n新浪微博地址: http://weibo.com/yangpu1983")]

        /// <summary>

        /// 每页显示数据条数

        /// <author>作者:杨璞</author>

        /// <CreateDataTime>创建日期:2012-09-26</CreateDataTime>

        /// <LastModefyTime>修改日期:2012-09-26</LastModefyTime>

        /// <WeiboAddress>新浪微博地址: http://weibo.com/yangpu1983</WeiboAddress>

        /// </summary>

        public int RowCount

        {

            get

            {

                if (ViewState["RowCount"] == null)

                    ViewState["RowCount"] = 0;

                return Convert.ToInt32(ViewState["RowCount"]);

            }

            set

            {

                ViewState["RowCount"] = value;

            }

        }



        [Category("数据"),DefaultValue(0), Description("页索引\n作者:杨璞\n创建日期:2012-09-26\n修改日期:2012-09-26\n新浪微博地址: http://weibo.com/yangpu1983")]

        /// <summary>

        /// 页索引

        /// <author>作者:杨璞</author>

        /// <CreateDataTime>创建日期:2012-09-26</CreateDataTime>

        /// <LastModefyTime>修改日期:2012-09-26</LastModefyTime>

        /// <WeiboAddress>新浪微博地址: http://weibo.com/yangpu1983</WeiboAddress>

        /// </summary>

        public int PageIndex

        {

            get

            {

                if (ViewState["PageIndex"] == null)

                    ViewState["PageIndex"] = 0;

                return Convert.ToInt32(ViewState["PageIndex"]);

            }

            set

            {

                ViewState["PageIndex"] = value;

            }

        }



        [Category("外观"), DefaultValue(1), Description("显示样式\n作者:杨璞\n创建日期:2012-09-26\n修改日期:2012-09-26\n新浪微博地址: http://weibo.com/yangpu1983")]

        public RenderType RenderStyle

        {

            get

            {

                if (ViewState["RenderStyle"] == null)

                    ViewState["RenderStyle"] = RenderType.Link;

                return (RenderType)ViewState["RenderStyle"];

            }

            set

            {

                ViewState["RenderStyle"] = value;

            }

        }





        [Category("数据"), Description("总页数\n作者:杨璞\n创建日期:2012-09-26\n修改日期:2012-09-26\n新浪微博地址: http://weibo.com/yangpu1983")]

        /// <summary>

        /// 总页数

        /// <author>作者:杨璞</author>

        /// <CreateDataTime>创建日期:2012-09-26</CreateDataTime>

        /// <LastModefyTime>修改日期:2012-09-26</LastModefyTime>

        /// <WeiboAddress>新浪微博地址: http://weibo.com/yangpu1983</WeiboAddress>

        /// </summary>

        public int PageCount

        {

            get

            {

                if (ViewState["PageCount"] == null)

                    if (this.PageSize != 0)

                        ViewState["PageCount"] = this.RowCount % this.PageSize == 0 ? this.RowCount / this.PageSize : (this.RowCount / this.PageSize) + 1;

                    else

                        ViewState["PageCount"] = 0;



                return Convert.ToInt32(ViewState["PageCount"]);

            }

            set

            {

                ViewState["PageCount"] = value;

            }

        }

    }

}

 

4.PagerEventHandler.cs

PagerEventHandler.cs
using System;

using System.Collections.Generic;

using System.Text;

using System.ComponentModel;



namespace Yangpu.Web.ExControls

{

    

    /// <summary>

    /// 分页控件事件委托原型

    /// <author>作者:杨璞</author>

    /// <CreateDataTime>创建日期:2012-09-26</CreateDataTime>

    /// <LastModefyTime>修改日期:2012-09-26</LastModefyTime>

    /// <WeiboAddress>新浪微博地址: http://weibo.com/yangpu1983</WeiboAddress>

    /// </summary>

    /// <param name="sender">事件源对象</param>

    /// <param name="e">分页控件事件处理参数</param>

    public delegate void PageIndexChangedEventHandler(object sender, PageIndexChangedEventArgs e);



    /// <summary>

    /// 分页控件呈现模式

    /// </summary>

    public enum RenderType

    {

        /// <summary>

        /// 链接模式

        /// </summary>

        Link = 1,

        /// <summary>

        /// 按钮模式

        /// </summary>

        Button = 2,

        /// <summary>

        /// 下拉列表模式

        /// </summary>

        DropDownList = 3

    }



    

    /// <summary>

    /// 分页控件事件处理参数类

    /// <author>作者:杨璞</author>

    /// <CreateDataTime>创建日期:2012-09-26</CreateDataTime>

    /// <LastModefyTime>修改日期:2012-09-26</LastModefyTime>

    /// <WeiboAddress>新浪微博地址: http://weibo.com/yangpu1983</WeiboAddress>

    /// </summary>

    public class PageIndexChangedEventArgs : EventArgs

    {

        public int PageIndex { get; set; }

    }

}

你可能感兴趣的:(asp.net)