亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

自定義控件

系統 1582 0
最近做項目見到別人寫的一個分頁控件不錯,記錄一下關于路徑的問題。
一、解決方案
二、添加類庫項目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文件

自定義控件


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 香蕉视频国产在线观看 | 99久久综合精品国产 | 人人干人人草 | 欧美色88 | 中国一级特黄大片毛片 | 香蕉视频成人 | 国产人成精品免费视频 | 国产91在线九色 | 久久综合资源 | 亚洲欧美婷婷 | 高清国产精品入口麻豆 | 99热久久国产精品免费观看 | 免费一级毛片在线播放放视频 | 911国产视频 | 欧美精品一区二区三区观 | 91亚洲视频在线观看 | 国产精品毛片一区 | 91中文字幕视频 | 玖玖中文 | 国产毛片久久国产 | julia中文在线| 久久综合九色欧美综合狠狠 | 精品国产你懂的在线观看 | 高清国产一区二区三区 | 女人18毛片a级毛片免费视频 | 亚洲免费大片 | 五月天色区 | 欧美洲久久日韩欧美 | 爱爱免费观看高清视频在线播放 | www操操 | 国产天堂 | 9l国产精品久久久久麻豆 | 色网站在线观看 | 精品国产日韩亚洲一区91 | 中文字幕视频在线播放 | 成人爽视频 | 日韩国产欧美 | 男人的天堂一区二区视频在线观看 | 一级特黄aaaaaa大片 | 四虎影视在线影院在线观看观看 | 久久精品阿娇 |