SdaiBuyWebUI PageControl.cs

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

namespace SdaiBuyWebUI
{
    [
        DefaultProperty("PageIndex"),
        ToolboxData("<{0}:PageControl runat=server></{0}:PageControl>")
    ]
    public class PageControl : WebControl
    {
        [
            Bindable(true),
            Category("数据"),
            DefaultValue("5"),
            Localizable(true),
            Description("每页条数")
        ]
        public int PageSize
        {
            get
            {
                if (ViewState["PageSize"] == null)
                {
                    return 5;
                }
                else
                {
                    return (int)ViewState["PageSize"];
                }
            }

            set
            {
                if (value > 0)
                {
                    ViewState["PageSize"] = value;
                }
            }
        }

        [
            Bindable(true),
            Category("数据"),
            DefaultValue("1"),
            Localizable(true),
            Description("当前页码")
        ]
        public int PageIndex
        {
            get
            {
                if (ViewState["PageIndex"] == null)
                {
                    return 1;
                }
                else
                {
                    return (int)ViewState["PageIndex"];
                }
            }

            set
            {
                if (PageCount == 0 || (value >= 1 && value <= PageCount))
                {
                    ViewState["PageIndex"] = value;
                }
            }
        }

        [
            Bindable(true),
            Category("数据"),
            DefaultValue("1"),
            Localizable(true),
            Description("总页数,设为0则由系统自动计算")
        ]
        public int PageCount
        {
            get
            {
                if (ViewState["PageCount"] == null)
                {
                    return 1;
                }
                else
                {
                    return (int)ViewState["PageCount"];
                }
            }

            set
            {
                if (value >= 0)
                {
                    ViewState["PageCount"] = value;
                }
            }
        }

        [
            Bindable(true),
            Category("数据"),
            DefaultValue("1"),
            Localizable(true),
            Description("数据总条数")
        ]
        public int Count
        {
            get
            {
                if (ViewState["Count"] == null)
                {
                    return 1;
                }
                else
                {
                    return (int)ViewState["Count"];
                }
            }

            set
            {
                if (value >= 0)
                {
                    ViewState["Count"] = value;
                }
            }
        }

        [
            Bindable(true),
            Category("数据"),
            DefaultValue("page"),
            Localizable(true),
            Description("页码参数")
        ]
        public string PageIndexParameter
        {
            get
            {
                if (ViewState["PageIndexParameter"] == null)
                {
                    return "page";
                }
                else
                {
                    return (string)ViewState["PageIndexParameter"];
                }
            }

            set
            {
                if (value.Trim() != string.Empty)
                {
                    ViewState["PageIndexParameter"] = value;
                }
            }
        }

        [
            Bindable(true),
            Category("数据"),
            DefaultValue("当前页Url"),
            Localizable(true),
            Description("页Url")
        ]
        public string PageUrl
        {
            get
            {
                if (ViewState["PageUrl"] == null)
                {
                    return string.Empty;
                }
                else
                {
                    return (string)ViewState["PageUrl"];
                }
            }

            set
            {
                ViewState["PageUrl"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("true"),
            Localizable(true),
            Description("是否显示基本信息")
        ]
        public bool ShowBasicInfo
        {
            get
            {
                if (ViewState["ShowBasicInfo"] == null)
                {
                    return true;
                }
                else
                {
                    return (bool)ViewState["ShowBasicInfo"];
                }
            }

            set
            {
                ViewState["ShowBasicInfo"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("true"),
            Localizable(true),
            Description("是否显示首页")
        ]
        public bool ShowFirstPageUrl
        {
            get
            {
                if (ViewState["ShowFirstPageUrl"] == null)
                {
                    return true;
                }
                else
                {
                    return (bool)ViewState["ShowFirstPageUrl"];
                }
            }

            set
            {
                ViewState["ShowFirstPageUrl"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("true"),
            Localizable(true),
            Description("是否显示上一页")
        ]
        public bool ShowPreviousPageUrl
        {
            get
            {
                if (ViewState["ShowPreviousPageUrl"] == null)
                {
                    return true;
                }
                else
                {
                    return (bool)ViewState["ShowPreviousPageUrl"];
                }
            }

            set
            {
                ViewState["ShowPreviousPageUrl"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("true"),
            Localizable(true),
            Description("是否显示下一页")
        ]
        public bool ShowNextPageUrl
        {
            get
            {
                if (ViewState["ShowNextPageUrl"] == null)
                {
                    return true;
                }
                else
                {
                    return (bool)ViewState["ShowNextPageUrl"];
                }
            }

            set
            {
                ViewState["ShowNextPageUrl"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("true"),
            Localizable(true),
            Description("是否显示最后一页")
        ]
        public bool ShowLastPageUrl
        {
            get
            {
                if (ViewState["ShowLastPageUrl"] == null)
                {
                    return true;
                }
                else
                {
                    return (bool)ViewState["ShowLastPageUrl"];
                }
            }

            set
            {
                ViewState["ShowLastPageUrl"] = value;
            }
        }

        [
            Bindable(true),
            Category("数据"),
            DefaultValue("0"),
            Localizable(true),
            Description("显示相邻几页,0为全部显示")
        ]
        public int NearPageCount
        {
            get
            {
                if (ViewState["NearPageCount"] == null)
                {
                    return 5;
                }
                else
                {
                    int i = (int)ViewState["NearPageCount"];
                    if (i >= 0)
                    {
                        return i;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }

            set
            {
                if (value >= 0)
                {
                    ViewState["NearPageCount"] = value;
                }
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("第{0}页/共{1}页,共{2}条记录"),
            Localizable(true),
            Description("基本信息显示方式"),
        ]
        public string BasicInfoFormat
        {
            get
            {
                if (ViewState["BasicInfoFormat"] == null)
                {
                    return "第{0}页/共{1}页,共{2}条记录";
                }
                else
                {
                    return (string)ViewState["BasicInfoFormat"];
                }
            }

            set
            {
                ViewState["BasicInfoFormat"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("{0}"),
            Localizable(true),
            Description("数字页显示方式"),
        ]
        public string PageInfoFormat
        {
            get
            {
                if (ViewState["PageInfoFormat"] == null)
                {
                    return "{0}";
                }
                else
                {
                    return (string)ViewState["PageInfoFormat"];
                }
            }

            set
            {
                ViewState["PageInfoFormat"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("未找到任何记录!"),
            Localizable(true),
            Description("无记录时显示的文字"),
        ]
        public string NoCountString
        {
            get
            {
                if (ViewState["NoCountString"] == null)
                {
                    return "未找到任何记录!";
                }
                else
                {
                    return (string)ViewState["NoCountString"];
                }
            }

            set
            {
                ViewState["NoCountString"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("true"),
            Localizable(true),
            Description("是否显示下拉框"),
        ]
        public bool ShowDropDownList
        {
            get
            {
                if (ViewState["ShowDropDownList"] == null)
                {
                    return true;
                }
                else
                {
                    return (bool)ViewState["ShowDropDownList"];
                }
            }

            set
            {
                ViewState["ShowDropDownList"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("true"),
            Localizable(true),
            Description("是否显示文本框"),
        ]
        public bool ShowTextBox
        {
            get
            {
                if (ViewState["ShowTextBox"] == null)
                {
                    return true;
                }
                else
                {
                    return (bool)ViewState["ShowTextBox"];
                }
            }

            set
            {
                ViewState["ShowTextBox"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("转到"),
            Localizable(true),
            Description("转到第几页的按钮上的文本"),
        ]
        public string ButtonText
        {
            get
            {
                if (ViewState["ButtonText"] == null)
                {
                    return "转到";
                }
                else
                {
                    return (string)ViewState["ButtonText"];
                }
            }

            set
            {
                ViewState["ButtonText"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("true"),
            Localizable(true),
            Description("未填入数字时,是否提示"),
        ]
        public bool ShowPrompt
        {
            get
            {
                if (ViewState["ShowPrompt"] == null)
                {
                    return true;
                }
                else
                {
                    return (bool)ViewState["ShowPrompt"];
                }
            }

            set
            {
                ViewState["ShowPrompt"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("请输入正确的页码!"),
            Localizable(true),
            Description("填写错误时提示的内容"),
        ]
        public string PromptText
        {
            get
            {
                if (ViewState["PromptText"] == null)
                {
                    return "请输入正确的页码!";
                }
                else
                {
                    return (string)ViewState["PromptText"];
                }
            }

            set
            {
                ViewState["PromptText"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("首页"),
            Localizable(true),
            Description("第一页显示的文本"),
        ]
        public string FirstPageText
        {
            get
            {
                if (ViewState["FirstPageText"] == null)
                {
                    return "首页";
                }
                else
                {
                    return (string)ViewState["FirstPageText"];
                }
            }

            set
            {
                ViewState["FirstPageText"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("上一页"),
            Localizable(true),
            Description("上一页显示的文本"),
        ]
        public string PreviousPageText
        {
            get
            {
                if (ViewState["PreviousPageText"] == null)
                {
                    return "上一页";
                }
                else
                {
                    return (string)ViewState["PreviousPageText"];
                }
            }

            set
            {
                ViewState["PreviousPageText"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("下一页"),
            Localizable(true),
            Description("下一页显示的文本"),
        ]
        public string NextPageText
        {
            get
            {
                if (ViewState["NextPageText"] == null)
                {
                    return "下一页";
                }
                else
                {
                    return (string)ViewState["NextPageText"];
                }
            }

            set
            {
                ViewState["NextPageText"] = value;
            }
        }

        [
            Bindable(true),
            Category("外观"),
            DefaultValue("末页"),
            Localizable(true),
            Description("末页显示的文本"),
        ]
        public string LastPageText
        {
            get
            {
                if (ViewState["LastPageText"] == null)
                {
                    return "末页";
                }
                else
                {
                    return (string)ViewState["LastPageText"];
                }
            }

            set
            {
                ViewState["LastPageText"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            if (Count > 0)
            {
                if (PageCount == 0)
                {
                    PageCount = (Count - 1) / PageSize + 1;
                }

                if (ShowBasicInfo)
                {
                    output.Write(string.Format(BasicInfoFormat, PageIndex, PageCount, Count));
                    output.Write("&nbsp;");
                }

                #region 链接选页
                if (ShowFirstPageUrl)
                {
                    WritePageUrl(output, 1, FirstPageText);
                    output.Write("&nbsp;");
                }
                if (ShowPreviousPageUrl)
                {
                    WritePageUrl(output, PageIndex - 1, PreviousPageText);
                    output.Write("&nbsp;");
                }

                if (NearPageCount == 0)
                {
                    for (int i = 1; i <= PageCount; i++)
                    {
                        if (i != PageIndex)
                        {
                            WritePageUrl(output, i, string.Format(PageInfoFormat, i));
                        }
                        else
                        {
                            output.Write(i.ToString());
                        }
                        output.Write("&nbsp;");
                    }
                }
                else
                {
                    for (int i = PageIndex - NearPageCount; i <= PageIndex + NearPageCount; i++)
                    {
                        if (i >= 1 && i <= PageCount)
                        {
                            if (i != PageIndex)
                            {
                                WritePageUrl(output, i, string.Format(PageInfoFormat, i));
                            }
                            else
                            {
                                output.Write(i.ToString());
                            }
                            output.Write("&nbsp;");
                        }
                    }
                }

                if (ShowNextPageUrl)
                {
                    WritePageUrl(output, PageIndex + 1, NextPageText);
                    output.Write("&nbsp;");
                }
                if (ShowLastPageUrl)
                {
                    WritePageUrl(output, PageCount, LastPageText);
                    output.Write("&nbsp;");
                }
                #endregion

                #region 下拉框选页
                if (ShowDropDownList)
                {
                    output.AddAttribute(HtmlTextWriterAttribute.Onchange, "javascript:location.href=this.value;");
                    output.RenderBeginTag(HtmlTextWriterTag.Select);
                    for (int i = 1; i <= PageCount; i++)
                    {
                        output.AddAttribute(HtmlTextWriterAttribute.Value, ReplaceParamentFromUrl(PageUrl, PageIndexParameter) + PageIndexParameter + "=" + i.ToString());
                        if (i == PageIndex)
                        {
                            output.AddAttribute(HtmlTextWriterAttribute.Selected, "selected");
                        }
                        output.RenderBeginTag(HtmlTextWriterTag.Option);
                        output.Write(string.Format(PageInfoFormat, i));
                        output.RenderEndTag();
                    }
                    output.RenderEndTag();
                    output.Write("&nbsp;");
                }
                #endregion

                #region 文本框选页
                if (ShowTextBox)
                {
                    output.AddAttribute(HtmlTextWriterAttribute.Id, UniqueID + "txtPageIndex");
                    output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
                    output.AddAttribute(HtmlTextWriterAttribute.Value, PageIndex.ToString());
                    output.AddAttribute("onkeypress", UniqueID + "IsNumber()");
                    output.AddStyleAttribute(HtmlTextWriterStyle.Width, "30px");
                    output.RenderBeginTag(HtmlTextWriterTag.Input);
                    output.RenderEndTag();

                    output.AddAttribute(HtmlTextWriterAttribute.Type, "button");
                    output.AddAttribute(HtmlTextWriterAttribute.Value, ButtonText);
                    output.AddAttribute(HtmlTextWriterAttribute.Onclick, UniqueID + "GoPage()");
                    output.RenderBeginTag(HtmlTextWriterTag.Input);
                    output.RenderEndTag();
                }
                #endregion
            }
            else
            {
                output.Write(NoCountString);
            }
        }

        /// <summary>
        /// 输出一个页的链接
        /// </summary>
        private void WritePageUrl(HtmlTextWriter writer, int pageIndex, string Descn)
        {
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            if (pageIndex > PageCount)
            {
                pageIndex = PageCount;
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Href, ReplaceParamentFromUrl(PageUrl, PageIndexParameter) + PageIndexParameter + "=" + pageIndex.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write(Descn);
            writer.RenderEndTag();
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);

            if (ShowTextBox)
            {
                writer.WriteLine(GetScript());
            }
        }

        protected string GetScript()
        {
            StringBuilder script = new StringBuilder();
            script.AppendLine("<script type=/"text/javascript/">");
            script.AppendLine(string.Format("function {0}IsNumber()", UniqueID));
            script.AppendLine("{");
            script.AppendLine("    if((event.keyCode>57||event.keyCode<48)&&event.keyCode!=46&&event.keyCode!=13)");
            script.AppendLine("          event.keyCode=0;");
            script.AppendLine("    if(event.keyCode==13)");
            script.AppendLine(string.Format("          {0}GoPage();", UniqueID));
            script.AppendLine("}");
            script.AppendLine(string.Format("function {0}GoPage()", UniqueID));
            script.AppendLine("{");
            script.AppendLine(string.Format("    var pageIndex=document.getElementById(/"{0}txtPageIndex/").value;", UniqueID));
            if (ShowPrompt)
            {
                script.AppendLine(string.Format("    if(pageIndex==/"/" || pageIndex<1 || pageIndex>{0})", PageCount));
                script.AppendLine(string.Format("        alert(/"{0}/");", PromptText));
                script.AppendLine("    else");
            }
            script.AppendLine(string.Format("        location=/"{0}{1}=/"+pageIndex;", ReplaceParamentFromUrl(PageUrl, PageIndexParameter), PageIndexParameter));
            script.AppendLine("}");
            script.AppendLine("</script>");
            return script.ToString();
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="pageIndex">当前第几页</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="count">数据条数</param>
        /// <param name="pageCount">页数</param>
        /// <param name="pageUrl">页Url,为Null表示当前页</param>
        /// <param name="pageIndexParameter">页Page参数</param>
        public void Bind(int pageIndex, int pageSize, int count, int pageCount, string pageUrl, string pageIndexParameter)
        {
            Count = count;
            PageSize = pageSize;
            PageCount = pageCount;
            PageIndex = pageIndex;
            if (!string.IsNullOrEmpty(pageUrl))
            {
                PageUrl = pageUrl;
            }
            else
            {
                PageUrl = this.Page.Request.Url.ToString();
            }
            PageIndexParameter = pageIndexParameter;
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="pageIndex">当前第几页</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="count">数据条数</param>
        /// <param name="pageUrl">页Url,为Null表示当前页</param>
        /// <param name="pageIndexParameter">页Page参数</param>
        public void Bind(int pageIndex, int pageSize, int count, string pageUrl, string pageIndexParameter)
        {
            Bind(pageIndex, pageSize, count, 0, pageUrl, pageIndexParameter);
        }

        /// <summary>
        /// 将标识第几页的参数提出来后,返回新的Url
        /// </summary>
        private string ReplaceParamentFromUrl(string url, string throwParameter)
        {
            string newUrl = url;

            if (url.IndexOf('?') > 0)
            {
                newUrl = url.Remove(url.IndexOf('?'));
                newUrl += "?";
                url = url.Substring(url.IndexOf('?') + 1);
                foreach (string parameter in url.Split(new Char[] { '&' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (parameter.Remove(parameter.IndexOf('=')).ToLower() != throwParameter.ToLower())
                        newUrl += parameter + "&";
                }
            }
            else
                newUrl += "?";
            return newUrl;
        }
    }
}

你可能感兴趣的:(SdaiBuyWebUI PageControl.cs)