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

namespace CustomWebControl.PageNavigation
{
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    public class PageNavigation2 : WebControl, IPostBackDataHandler, IPostBackEventHandler
    {
        public const string PAGE_CURRENT = "pageCurrent";
        public const string PAGE_SUM = "pageSum";
        public const string CSS_CLASS = "cssClass";
        public const string SEARCH_STATE = "searchState";
        public const string PAGE_COUNT = "pageCount";

        [Bindable(true), Category("数据"), DefaultValue(1), Description("当前页码")]
        public int PageCurrent
        {
            get
            {
                int pageCurrent = null == ViewState[PAGE_CURRENT] ? 1 : (int)ViewState[PAGE_CURRENT];
                return pageCurrent;
            }
            set
            {
                ViewState[PAGE_CURRENT] = value;
            }
        }

        [Bindable(true), Category("数据"), DefaultValue(0), Description("总页数")]
        public int PageSum
        {
            get
            {
                int pageSum = null == ViewState[PAGE_SUM] ? 0 : (int)ViewState[PAGE_SUM];
                return pageSum;
            }
            set
            {
                ViewState[PAGE_SUM] = value;
            }
        }

        [Bindable(true), Category("外观"), DefaultValue(""), Description("Css Class名称")]
        public override string CssClass
        {
            get
            {
                string str = null == ViewState[CSS_CLASS] ? string.Empty : ViewState[CSS_CLASS].ToString();
                return str;
            }
            set
            {
                ViewState[CSS_CLASS] = value;
            }
        }

        [Bindable(true), Category("外观"), DefaultValue(""), Description("页码显示数")]
        public int PageCount
        {
            get
            {
                int count = null == ViewState[PAGE_COUNT] ? 5 : int.Parse(ViewState[PAGE_COUNT].ToString());
                return count;
            }
            set
            {
                ViewState[PAGE_COUNT] = value;
            }
        }

        //事件键定义
        private static readonly object _event = new object();

        /// <summary>
        /// 当前页改变事件
        /// </summary>
        [Category("事件"), Description("当前页码改变时触发此事件")]
        public event EventHandler ChangePageCurrent
        {
            add
            {
                Events.AddHandler(_event, value);
            }
            remove
            {
                Events.RemoveHandler(_event, value);
            }
        }

        /// <summary>
        /// 当前页改变的事件方法
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnChangePageCurrent(EventArgs e)
        {
            EventHandler handler = (EventHandler)Events[_event];

            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            if (this.PageSum != 0 && this.PageCurrent > this.PageSum)
                this.PageCurrent = this.PageSum;
            if (this.PageSum != 0 && this.PageCurrent < 1)
                this.PageCurrent = 1;

            if (this.PageSum <= 0)
            {
                output.AddAttribute(HtmlTextWriterAttribute.Height, "30");
                output.AddAttribute(HtmlTextWriterAttribute.Border, "0");
                output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
                output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
                output.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass == string.Empty ? "font12hei" : this.CssClass);
                output.AddAttribute(HtmlTextWriterAttribute.Style, "margin-top:4px");
                output.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
                output.RenderBeginTag(HtmlTextWriterTag.Table);
                output.RenderBeginTag(HtmlTextWriterTag.Tr);
                output.AddAttribute(HtmlTextWriterAttribute.Align, "center");
                output.RenderBeginTag(HtmlTextWriterTag.Td);
                output.Write("没有查询到符合条件的数据。");
                output.RenderEndTag();
                output.RenderEndTag();
                output.RenderEndTag();
                return;
            }
            output.AddAttribute(HtmlTextWriterAttribute.Height, "30");
            output.AddAttribute(HtmlTextWriterAttribute.Border, "0");
            output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
            output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            output.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass == string.Empty ? "font12hei" : this.CssClass);
            output.AddAttribute(HtmlTextWriterAttribute.Style, "margin-top:4px");
            output.RenderBeginTag(HtmlTextWriterTag.Table);
            output.RenderBeginTag(HtmlTextWriterTag.Tr);
            output.AddAttribute(HtmlTextWriterAttribute.Width, "110");
            output.AddAttribute(HtmlTextWriterAttribute.Align, "left");
            output.RenderBeginTag(HtmlTextWriterTag.Td);
            output.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            output.AddAttribute(HtmlTextWriterAttribute.Border, "0");
            output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
            output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            output.AddAttribute(HtmlTextWriterAttribute.Height, "2");
            output.RenderBeginTag(HtmlTextWriterTag.Table);
            output.RenderBeginTag(HtmlTextWriterTag.Tr);
            output.RenderBeginTag(HtmlTextWriterTag.Td);
            output.RenderEndTag();//</td>
            output.RenderEndTag();//</tr>
            output.RenderEndTag();//</table>
            output.AddAttribute(HtmlTextWriterAttribute.Height, "18");
            output.AddAttribute(HtmlTextWriterAttribute.Title, "首 页");
            output.AddAttribute(HtmlTextWriterAttribute.Style, "cursor:pointer");
            if (this.PageCurrent == 1 || this.PageSum == 0)
            {
                output.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                output.AddAttribute(HtmlTextWriterAttribute.Src, "http://www.cnblogs.com/images/sy.gif");
            }
            else
            {
                output.AddAttribute(HtmlTextWriterAttribute.Src, "http://www.cnblogs.com/images/sy1.gif");
                output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, "First"));
            }
            output.RenderBeginTag(HtmlTextWriterTag.Img);
            output.RenderEndTag();
            output.Write("&nbsp;");
            output.AddAttribute(HtmlTextWriterAttribute.Height, "18");
            output.AddAttribute(HtmlTextWriterAttribute.Title, "上一页");
            output.AddAttribute(HtmlTextWriterAttribute.Style, "cursor:pointer");
            if (this.PageCurrent == 1 || this.PageSum == 0)
            {
                output.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                output.AddAttribute(HtmlTextWriterAttribute.Src, "http://www.cnblogs.com/images/pre_page_simple.gif");
            }
            else
            {
                output.AddAttribute(HtmlTextWriterAttribute.Src, "http://www.cnblogs.com/images/pre_page_simple1.gif");
                output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, "Previous"));
            }
            output.RenderBeginTag(HtmlTextWriterTag.Img);
            output.RenderEndTag();
            output.RenderEndTag();//</td>
            output.AddAttribute(HtmlTextWriterAttribute.Width, "100");
            output.AddAttribute(HtmlTextWriterAttribute.Align, "center");
            output.AddAttribute(HtmlTextWriterAttribute.Valign, "middle");
            output.AddAttribute(HtmlTextWriterAttribute.Style, "font-size:13px; font-family: Arial, Helvetica, sans-serif;");
            output.RenderBeginTag(HtmlTextWriterTag.Td);

            if (this.PageCurrent <= this.PageCount)
            {
                for (int i = 1; i <= this.PageCount; i++)
                {
                    if (i > this.PageSum)
                        break;
                    if (i == this.PageCurrent)
                    {
                        output.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                    }
                    else
                    {
                        output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, i.ToString()));
                    }
                    output.AddAttribute(HtmlTextWriterAttribute.Style, "cursor:pointer");
                    output.AddAttribute("onmouseover", "this.style.textDecoration='underline';");
                    output.AddAttribute("onmouseout", "this.style.textDecoration='none'");
                    output.RenderBeginTag(HtmlTextWriterTag.Span);
                    output.Write(i.ToString());
                    output.RenderEndTag();
                    output.Write("&nbsp;");
                }
            }
            else if (this.PageCurrent >= this.PageSum - (this.PageSum % this.PageCount))
            {
                for (int i = (this.PageCurrent % this.PageCount == 0 ? (this.PageCurrent / this.PageCount - 1) : (this.PageCurrent / this.PageCount)) * this.PageCount + 1; i <= (this.PageCurrent % this.PageCount == 0 ? (this.PageCurrent / this.PageCount - 1) : (this.PageCurrent / this.PageCount)) * this.PageCount + this.PageCount; i++)
                {
                    if (i > this.PageSum)
                        break;
                    if (i == this.PageCurrent)
                    {
                        output.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                    }
                    else
                    {
                        output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, i.ToString()));
                    }
                    output.AddAttribute(HtmlTextWriterAttribute.Style, "cursor:pointer");
                    output.AddAttribute("onmouseover", "this.style.textDecoration='underline';");
                    output.AddAttribute("onmouseout", "this.style.textDecoration='none'");
                    output.RenderBeginTag(HtmlTextWriterTag.Span);
                    output.Write(i.ToString());
                    output.RenderEndTag();
                    output.Write("&nbsp;");
                }
            }
            else
            {
                int start = this.PageCurrent % this.PageCount == 0 ? (this.PageCurrent / this.PageCount - 1) * this.PageCount + 1 : (this.PageCurrent / this.PageCount) * this.PageCount + 1;
                for (int i = start; i < start + this.PageCount; i++)
                {
                    if (i > this.PageSum)
                        break;
                    if (i == this.PageCurrent)
                    {
                        output.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                    }
                    else
                    {
                        output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, i.ToString()));
                    }
                    output.AddAttribute(HtmlTextWriterAttribute.Style, "cursor:pointer");
                    output.AddAttribute("onmouseover", "this.style.textDecoration='underline';");
                    output.AddAttribute("onmouseout", "this.style.textDecoration='none'");
                    output.RenderBeginTag(HtmlTextWriterTag.Span);
                    output.Write(i.ToString());
                    output.RenderEndTag();
                    output.Write("&nbsp;");
                }
            }


            output.RenderEndTag();//</td>
            output.AddAttribute(HtmlTextWriterAttribute.Width, "110");
            output.AddAttribute(HtmlTextWriterAttribute.Align, "left");
            output.RenderBeginTag(HtmlTextWriterTag.Td);
            output.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            output.AddAttribute(HtmlTextWriterAttribute.Border, "0");
            output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
            output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            output.AddAttribute(HtmlTextWriterAttribute.Height, "2");
            output.RenderBeginTag(HtmlTextWriterTag.Table);
            output.RenderBeginTag(HtmlTextWriterTag.Tr);
            output.RenderBeginTag(HtmlTextWriterTag.Td);
            output.RenderEndTag();//</td>
            output.RenderEndTag();//</tr>
            output.RenderEndTag();//</table>
            output.AddAttribute(HtmlTextWriterAttribute.Height, "18");
            output.AddAttribute(HtmlTextWriterAttribute.Title, "下一页");
            output.AddAttribute(HtmlTextWriterAttribute.Style, "cursor:pointer");
            if (this.PageCurrent == this.PageSum || this.PageSum == 0)
            {
                output.AddAttribute(HtmlTextWriterAttribute.Src, "http://www.cnblogs.com/images/next_page.gif");
                output.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
            }
            else
            {
                output.AddAttribute(HtmlTextWriterAttribute.Src, "http://www.cnblogs.com/images/next_page1.gif");
                output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, "Next"));
            }
            output.RenderBeginTag(HtmlTextWriterTag.Img);
            output.RenderEndTag();//</img>
            output.Write("&nbsp;");
            output.AddAttribute(HtmlTextWriterAttribute.Height, "18");
            output.AddAttribute(HtmlTextWriterAttribute.Title, "尾 页");
            output.AddAttribute(HtmlTextWriterAttribute.Style, "cursor:pointer");
            if (this.PageCurrent == this.PageSum || this.PageSum == 0)
            {
                output.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
                output.AddAttribute(HtmlTextWriterAttribute.Src, "http://www.cnblogs.com/images/wy.gif");
            }
            else
            {
                output.AddAttribute(HtmlTextWriterAttribute.Src, "http://www.cnblogs.com/images/wy1.gif");
                output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this, "Last"));
            }
            output.RenderBeginTag(HtmlTextWriterTag.Img);
            output.RenderEndTag();//</img>
            output.RenderEndTag();//</td>

            output.AddAttribute(HtmlTextWriterAttribute.Width, "3");
            output.RenderBeginTag(HtmlTextWriterTag.Td);
            output.RenderEndTag();//</td>

            output.AddAttribute(HtmlTextWriterAttribute.Align, "center");
            output.AddAttribute(HtmlTextWriterAttribute.Valign, "middle");
            output.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass == string.Empty ? "font12hei" : this.CssClass);
            output.RenderBeginTag(HtmlTextWriterTag.Td);
            output.Write("页次:" + this.PageCurrent + "/" + this.PageSum + "&nbsp;");
            output.RenderEndTag();//</td>

            output.AddAttribute(HtmlTextWriterAttribute.Width, "30");
            output.AddAttribute(HtmlTextWriterAttribute.Align, "center");
            output.AddAttribute(HtmlTextWriterAttribute.Valign, "middle");
            output.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass == string.Empty ? "font12hei" : this.CssClass);
            output.RenderBeginTag(HtmlTextWriterTag.Td);
            output.Write("转到");
            output.RenderEndTag();//</td>
            output.AddAttribute(HtmlTextWriterAttribute.Width, "35");
            output.AddAttribute(HtmlTextWriterAttribute.Align, "center");
            output.AddAttribute(HtmlTextWriterAttribute.Valign, "middle");
            output.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass == string.Empty ? "font12hei" : this.CssClass);
            output.RenderBeginTag(HtmlTextWriterTag.Td);
            output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            output.AddAttribute(HtmlTextWriterAttribute.Size, "3");
            output.AddAttribute(HtmlTextWriterAttribute.Style, "width:30px;height:20px");
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID + "_Page");
            output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            output.RenderEndTag();//</input>
            output.RenderEndTag();//</td>
            output.AddAttribute(HtmlTextWriterAttribute.Width, "20");
            output.AddAttribute(HtmlTextWriterAttribute.Align, "left");
            output.AddAttribute(HtmlTextWriterAttribute.Valign, "middle");
            output.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass == string.Empty ? "font12hei" : this.CssClass);
            output.RenderBeginTag(HtmlTextWriterTag.Td);
            output.Write("页");
            output.RenderEndTag();//</td>
            output.AddAttribute(HtmlTextWriterAttribute.Width, "100");
            output.AddAttribute(HtmlTextWriterAttribute.Align, "left");
            output.RenderBeginTag(HtmlTextWriterTag.Td);

            //output.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            //output.AddAttribute(HtmlTextWriterAttribute.Border, "0");
            //output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
            //output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            //output.AddAttribute(HtmlTextWriterAttribute.Height, "2");
            //output.RenderBeginTag(HtmlTextWriterTag.Table);
            //output.RenderBeginTag(HtmlTextWriterTag.Tr);
            //output.RenderBeginTag(HtmlTextWriterTag.Td);
            //output.RenderEndTag();//</td>
            //output.RenderEndTag();//</tr>
            //output.RenderEndTag();//</table>
            output.AddAttribute(HtmlTextWriterAttribute.Src, "http://www.cnblogs.com/images/btn_ok1.gif");
            output.AddAttribute(HtmlTextWriterAttribute.Title, "转到指定页");
            output.AddAttribute(HtmlTextWriterAttribute.Style, "cursor:pointer");
            output.AddAttribute(HtmlTextWriterAttribute.Onclick, "if(document.getElementById('" + this.UniqueID + "_Page').value<=" + this.PageSum + " && document.getElementById('" + this.UniqueID + "_Page').value>0) " + Page.ClientScript.GetPostBackEventReference(this, "Go"), false);
            output.RenderBeginTag(HtmlTextWriterTag.Img);
            output.RenderEndTag();//</img>
            output.RenderEndTag();//</td>
            output.RenderEndTag();//</tr>
            output.RenderEndTag();//</table>
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (Page != null)
            {
                Page.VerifyRenderingInServerForm(this);
            }

            base.Render(writer);
        }

        void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
        {
            switch (eventArgument)
            {
                case "First":
                    if (this.PageCurrent == 1)
                        return;
                    this.PageCurrent = 1;
                    OnChangePageCurrent(EventArgs.Empty);
                    break;
                case "Previous":
                    if (this.PageCurrent > 1)
                        this.PageCurrent = this.PageCurrent - 1;
                    else
                        return;
                    OnChangePageCurrent(EventArgs.Empty);
                    break;
                case "Next":
                    if (this.PageCurrent < this.PageSum)
                        this.PageCurrent = this.PageCurrent + 1;
                    else
                        return;
                    OnChangePageCurrent(EventArgs.Empty);
                    break;
                case "Last":
                    if (this.PageCurrent == this.PageSum)
                        return;
                    this.PageCurrent = this.PageSum;
                    OnChangePageCurrent(EventArgs.Empty);
                    break;
                case "Go":
                    if (this.PageCurrent < this.PageSum)
                        OnChangePageCurrent(EventArgs.Empty);
                    break;
                default:
                    this.PageCurrent = int.Parse(eventArgument);
                    OnChangePageCurrent(EventArgs.Empty);
                    break;
            }
        }

        public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            int pageCurrentValue = PageCurrent;

            int pageCurrentPostedValue;

            try
            {
                pageCurrentPostedValue = int.Parse(postCollection[postDataKey]);

                if (pageCurrentValue != pageCurrentPostedValue && pageCurrentPostedValue <= PageSum && pageCurrentPostedValue > 0)
                {
                    this.PageCurrent = pageCurrentPostedValue;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        public void RaisePostDataChangedEvent()
        {

        }
    }
}

posted on 2009-03-30 10:36  雨季  阅读(278)  评论(0编辑  收藏  举报