`
njmnjx
  • 浏览: 232256 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

自定义控件

阅读更多
最近做项目见到别人写的一个分页控件不错,记录一下关于路径的问题。
一、解决方案
二、添加类库项目test
三、添加引用System.Web
四、test下添加类文件Pagination.cs
五、test下添加样式文件Pagination.css(右键此文件--属性--生成操作--嵌入的资源)
六、Pagination.cs文件下添加如下内容
using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

//这里是引用嵌入资源的路径  项目名称.文件夹.文件名
[assembly: WebResource("test.css.Pagination.css", "css")]
namespace Common  //命名空间任意
{
    public class Pagination : WebControl, INamingContainer, IPostBackEventHandler
    {
        public enum PaginationMode
        {
            Get,
            Post
        }

        #region = 1 Count =
        /// <summary>
        /// 内容总条数
        /// </summary>
        public int Count
        {
            get { return ViewState["Count"] != null ? (int)ViewState["Count"] : 0; }
            set { ViewState["Count"] = value; }
        }
        #endregion

        #region = 2 PageNum =
        /// <summary>
        /// 每页内容数
        /// </summary>
        public int PageNum
        {
            get { return ViewState["PageNum"] != null ? (int)ViewState["PageNum"] : 20; }
            set { ViewState["PageNum"] = value; }
        }
        #endregion

        #region = 3 CurrentPageIndex =
        /// <summary>
        /// 当前页数
        /// </summary>
        public int CurrentPageIndex
        {
            get
            {
                int currentpageindex = 1;
                if (ViewState["CurrentPageIndex"] != null)
                {
                    int.TryParse(ViewState["CurrentPageIndex"].ToString(), out currentpageindex);
                }
                else
                {
                    if (this.Page.Request.QueryString["pages"] != null)
                    {
                        int.TryParse(this.Page.Request.QueryString["pages"].ToString(), out currentpageindex);
                    }
                }
                currentpageindex = currentpageindex < 1 ? 1 : currentpageindex;
                currentpageindex = currentpageindex > PageCount ? PageCount : currentpageindex;
                return currentpageindex;
            }
            set { ViewState["CurrentPageIndex"] = value; }
        }
        #endregion

        #region = 4 PageCount =
        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get { return (Count / PageNum) + ((Count % PageNum) == 0 ? 0 : 1); }
        }
        #endregion

        #region = 5 CssUrl =
        /// <summary>
        /// 分页样式
        /// </summary>
        public string CssUrl
        {
            get { return ViewState["CssUrl"] != null ? ViewState["CssUrl"].ToString() : this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "test.css.Pagination.css"); }  //项目名称.文件夹.文件名
            set { ViewState["CssUrl"] = value; }
        }
        #endregion

        #region = 6 Mode =
        /// <summary>
        /// Pagination的模式
        /// </summary>
        public PaginationMode Mode
        {
            get
            {
                object obj = this.ViewState["Mode"];
                return ((obj == null) ? PaginationMode.Get : ((PaginationMode)obj));
            }
            set
            {
                this.ViewState["Mode"] = value;
            }
        }
        #endregion

        #region = 7 PagesName =
        /// <summary>
        /// 分页样式
        /// </summary>
        public string PagesName
        {
            get { return ViewState["PagesName"] != null ? ViewState["PagesName"].ToString() : "pages"; }
            set { ViewState["PagesName"] = value; }
        }
        #endregion

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (this.PageCount > 1)
            {
                if ((this.Page != null) && (this.Page.Header != null))
                {
                    string cssKey = "PaginationCss";
                    if (this.Page.Header.FindControl(cssKey) == null)
                    {
                        HtmlLink child = new HtmlLink();
                        child.ID = cssKey;
                        child.Href = CssUrl;
                        child.Attributes["type"] = "text/css";
                        child.Attributes["rel"] = "stylesheet";
                        this.Page.Header.Controls.Add(child);
                    }
                    this.CssClass = "pagenumber";
                }
            }
        }

        #region = RenderContents =
        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);

            if (this.PageCount > 1)
            {
                writer.Write("共");
                writer.Write(Count);
                writer.Write("条");
                writer.Write("&nbsp;&nbsp;");
                writer.Write("每页");
                writer.Write(PageNum);
                writer.Write("条");
                writer.Write("&nbsp;&nbsp;");
                writer.Write("共");
                writer.Write(PageCount);
                writer.Write("页");
                writer.Write("&nbsp;&nbsp;");
                writer.Write("当前为第");
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "f90");
                writer.RenderBeginTag(HtmlTextWriterTag.B);
                writer.Write(CurrentPageIndex);
                writer.RenderEndTag();
                writer.Write("页");
                writer.Write("&nbsp;&nbsp;");
                writer.Write("选择页数:");

                for (int i = 1; i < 5; i++)
                {
                    if ((CurrentPageIndex - 5 + i) > 0)
                    {
                        if (this.Mode == PaginationMode.Get)
                        {
                            if (HttpContext.Current.Request.RawUrl.ToString().Contains(PagesName + "="))
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, Regex.Replace(HttpContext.Current.Request.RawUrl.ToString(), "(.+" + PagesName + "=)(\\d+)(.*)", "$1") + (CurrentPageIndex - 5 + i).ToString() + Regex.Replace(HttpContext.Current.Request.RawUrl.ToString(), "(.+" + PagesName + "=)(\\d+)(.*)", "$3"));
                            }
                            else if (HttpContext.Current.Request.QueryString.Count != 0)
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, HttpContext.Current.Request.RawUrl.ToString() + "&" + PagesName + "=" + (CurrentPageIndex - 5 + i).ToString());
                            }
                            else
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, "?" + PagesName + "=" + (CurrentPageIndex - 5 + i).ToString());
                            }
                            writer.AddAttribute(HtmlTextWriterAttribute.Class, "pagenumber");
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                            writer.Write((CurrentPageIndex - 5 + i).ToString());
                            writer.RenderEndTag();
                        }
                        else
                        {
                            PostBackOptions postBackOptions = this.GetPostBackOptions(CurrentPageIndex - 5 + i);
                            string postBackEventReference = null;
                            if (postBackOptions != null)
                            {
                                postBackEventReference = this.Page.ClientScript.GetPostBackEventReference(postBackOptions, true);
                            }
                            if (string.IsNullOrEmpty(postBackEventReference))
                            {
                                postBackEventReference = "javascript:void(0)";
                            }
                            writer.AddAttribute(HtmlTextWriterAttribute.Href, postBackEventReference);
                            writer.AddAttribute(HtmlTextWriterAttribute.Class, "pagenumber");
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                            writer.Write((CurrentPageIndex - 5 + i).ToString());
                            writer.RenderEndTag();
                        }
                    }
                }

                writer.AddAttribute(HtmlTextWriterAttribute.Class, "current");
                writer.RenderBeginTag(HtmlTextWriterTag.B);
                writer.Write(CurrentPageIndex);
                writer.RenderEndTag();

                for (int i = 1; i < 5; i++)
                {
                    if ((CurrentPageIndex + i) <= PageCount)
                    {
                        if (this.Mode == PaginationMode.Get)
                        {
                            if (HttpContext.Current.Request.RawUrl.ToString().Contains(PagesName + "="))
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, Regex.Replace(HttpContext.Current.Request.RawUrl.ToString(), "(.+" + PagesName + "=)(\\d+)(.*)", "$1") + (CurrentPageIndex + i).ToString() + Regex.Replace(HttpContext.Current.Request.RawUrl.ToString(), "(.+" + PagesName + "=)(\\d+)(.*)", "$3"));
                            }
                            else if (HttpContext.Current.Request.QueryString.Count != 0)
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, HttpContext.Current.Request.RawUrl.ToString() + "&" + PagesName + "=" + (CurrentPageIndex + i).ToString());
                            }
                            else
                            {
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, "?" + PagesName + "=" + (CurrentPageIndex + i).ToString());
                            }
                            writer.AddAttribute(HtmlTextWriterAttribute.Class, "pagenumber");
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                            writer.Write((CurrentPageIndex + i).ToString());
                            writer.RenderEndTag();
                        }
                        else
                        {
                            PostBackOptions postBackOptions = this.GetPostBackOptions(CurrentPageIndex + i);
                            string postBackEventReference = null;
                            if (postBackOptions != null)
                            {
                                postBackEventReference = this.Page.ClientScript.GetPostBackEventReference(postBackOptions, true);
                            }
                            if (string.IsNullOrEmpty(postBackEventReference))
                            {
                                postBackEventReference = "javascript:void(0)";
                            }
                            writer.AddAttribute(HtmlTextWriterAttribute.Href, postBackEventReference);
                            writer.AddAttribute(HtmlTextWriterAttribute.Class, "pagenumber");
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                            writer.Write((CurrentPageIndex + i).ToString());
                            writer.RenderEndTag();
                        }
                    }
                }

                if ((CurrentPageIndex + 4) < PageCount)
                {
                    writer.Write("...");
                }

                if (this.Mode == PaginationMode.Get)
                {
                    writer.Write("&nbsp;&nbsp;");
                    writer.Write("跳转到第");
                    writer.Write("<input id=\"" + this.UniqueID + "\" type=\"text\" onkeypress=\"if(event.keyCode==13){location.href='");
                    if (HttpContext.Current.Request.RawUrl.ToString().Contains("pages="))
                    {
                        writer.Write(Regex.Replace(HttpContext.Current.Request.RawUrl.ToString(), "(.+" + PagesName + "=)([\\w\\W^&]*)(.*)", "$1") + "'+this.value+'" + Regex.Replace(HttpContext.Current.Request.RawUrl.ToString(), "(.+" + PagesName + "=)([\\w\\W^&]*)(.*)", "$3") + "'");
                    }
                    else if (HttpContext.Current.Request.QueryString.Count != 0)
                    {
                        writer.Write(HttpContext.Current.Request.RawUrl.ToString() + "&" + PagesName + "='+this.value");
                    }
                    else
                    {
                        writer.Write(HttpContext.Current.Request.RawUrl.ToString() + "?" + PagesName + "='+this.value");
                    }
                    writer.Write(";return false;}\" class=\"page_input\"/>");
                    writer.Write("页");
                }
            }
        }
        #endregion

        private PostBackOptions GetPostBackOptions(int index)
        {
            PostBackOptions options = new PostBackOptions(this, index.ToString());
            options.RequiresJavaScriptProtocol = true;
            return options;
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            int num;
            if (int.TryParse(eventArgument, out num))
            {
                this.CurrentPageIndex = num;
            }
        }
    }
}


七、注:类文件的命名空间任意,嵌入资源路径要注意。
八、结构图如下:



九、vs工具箱--添加选项卡--选择荐--浏览(上面生成的dll文件)--确定完成。
或者:将dll文件拖曳入vs工具箱中即可。
十、拖曳使用控件。
十一、例句如下:
//头部自动生成引用
<%@ Register Assembly="test" Namespace="Common" TagPrefix="cc1" %>

<!--控件拖入此页面自动生成如下内容-->
    <cc1:Pagination ID="Pagination1" runat="server" />

//后台代码样例
protected void Page_Load(object sender, EventArgs e)
{
    Pagination1.Count = 1000;
    Pagination1.PageNum = 10;
}

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    //注:Repeater控件rptProblemCollection的数据源
     //Pagination分页控件ID:UPProblemCollectionPagination
    //PageNum 页容量
    //CurrentPageIndex当前页下标,注意sql拼写时判断CurrentPageIndex<=0时为1
    if (isMyQuestion)
       rptProblemCollection.DataSource = MeetingQuestion.GetAdvanceQuestion(UPProblemCollectionPagination.CurrentPageIndex, UPProblemCollectionPagination.PageNum, MeetingID, CurrentUserID, AUserID, KeyString);
    else
       rptProblemCollection.DataSource = MeetingQuestion.GetAdvanceQuestion(UPProblemCollectionPagination.CurrentPageIndex, UPProblemCollectionPagination.PageNum, MeetingID, 0, AUserID, KeyString);
       rptProblemCollection.DataBind();

    }


十二、效果图:




十三、附件:编译完成可用的 dll文件
  • 大小: 7.6 KB
  • 大小: 17.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics