自定义的asp.net翻页控件,利用RenderContents事件,动态生成页码,

并用JAVASCRIPT触发后台翻页事件
用脚 本触发事件得实例接口:IPostBackEventHandler  
其下有一个方法public void RaisePostBackEvent(string eventArgument)  
 
也就是当点击事件:output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this"__sp_TurnPage_" + preIndex + "_" + PageCount));
传入参数。。分析参数就可以做自已想要的事情。
 
 
自定义控件不了解的可以看:http://www.jiamaocode.com/Cts/1035.html
自定义事件不懂的可以看另一篇:http://www.jiamaocode.com/Cts/1150.html
 
自定义的asp.net翻页控件
 
C# 代码
  • [DefaultProperty("Text")]  
  •     [ToolboxData("<{0}:splitpage runat=server></{0}:splitpage>")]  
  •     public class splitpage : WebControl,IPostBackEventHandler  
  •     {  
  •         private int _SelectPageCount = 10;  
  •         [Bindable(true)]  
  •         [Category("SelectPageCount")]  
  •         [DefaultValue("10")]  
  •         [Localizable(true)]  
  •         public int SelectPageCount {  
  •             get {  
  •                 return _SelectPageCount;  
  •             }  
  •             set { _SelectPageCount = value; }  
  •         }  
  •   
  •         private int _PageIndex = 1;  
  •         [Bindable(true)]  
  •         [Category("PageIndex")]  
  •         [DefaultValue("10")]  
  •         [Localizable(true)]  
  •         public int PageIndex  
  •         {  
  •             get  
  •             {  
  •                 return _PageIndex;  
  •             }  
  •             set { _PageIndex = value; }  
  •         }  
  •   
  •         private int _PageCount = 1;  
  •         [Bindable(true)]  
  •         [Category("PageCount")]  
  •         [DefaultValue("1")]  
  •         [Localizable(true)]  
  •         public int PageCount  
  •         {  
  •             get  
  •             {  
  •                 return _PageCount;  
  •             }  
  •             set { _PageCount = value; }  
  •         }  
  •   
  •         string _pageindexCss = "PageIndexCSS";  
  •         /// <summary>  
  •         /// 翻页的样式  
  •         /// </summary>  
  •         [Bindable(true)]  
  •         [Category("PageIndexCss")]  
  •         [DefaultValue("1")]  
  •         [Localizable(true)]  
  •         public string PageIndexCss  
  •         {  
  •             get { return _pageindexCss; }  
  •             set { _pageindexCss = value; }  
  •         }  
  •         string _CurPageindexCss = "PageCurIndexCSS";  
  •         /// <summary>  
  •         /// 当前页的样式  
  •         /// </summary>  
  •         [Bindable(true)]  
  •         [Category("CurPageIndexCss")]  
  •         [DefaultValue("1")]  
  •         [Localizable(true)]  
  •         public string CurPageIndexCss  
  •         {  
  •             get { return _CurPageindexCss; }  
  •             set { _CurPageindexCss = value; }  
  •         }  
  •   
  •         public delegate void DelegatePageIndexChange(object sender,int pageIndex,int pageCount);  
  •         public event DelegatePageIndexChange PageIndexChangeHanlder;  
  •   
  •         //重写默认的标签  
  •         protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Div; } }       
  •     
  •         protected override void RenderContents(HtmlTextWriter output)  
  •         {  
  •             //output.AddAttribute(HtmlTextWriterAttribute.Onclick,Page.ClientScript.GetPostBackEventReference(this,"test"));  
  •             //output.RenderBeginTag(HtmlTextWriterTag.Div);  
  •             //output.Write("test");  
  •             //output.RenderEndTag();  
  •   
  •             //首先写入翻页样式  
  •             if (CurPageIndexCss == "PageCurIndexCSS" || PageIndexCss == "PageIndexCSS")  
  •             {  
  •                 output.AddAttribute(HtmlTextWriterAttribute.Type"text/css");  
  •                 output.RenderBeginTag(HtmlTextWriterTag.Style);  
  •                 output.Write("a.PageIndexCSS{padding: 2px;padding-left:4px;padding-right:4px;background: #fff;border: 1px solid #9AAFE5;text-decoration: none;color: #2452ac;margin-right:6px;" +  
  •         "cursor: pointer;}a.PageIndexCSS:link{color: #2452ac;text-decoration: none;cursor: pointer;}a.PageIndexCSS:visited{color: #2452ac;text-decoration: none;cursor: pointer;}a.PageIndexCSS:active" +  
  •     "{color: #2452ac;text-decoration: underline;cursor: pointer;}a.PageIndexCSS:hover{border: 1px solid #2452ac;color: black;text-decoration: underline;cursor: pointer;}a.PageCurIndexCSS{padding: 2px;" +  
  •        "padding-left:4px;padding-right:4px;border: 1px solid #2452ac;background-color: #2452ac;text-decoration: none;color: #fff;margin-right:6px;}");  
  •                 output.RenderEndTag();  
  •             }  
  •             var showPagesList = new List<int>();  
  •             showPagesList.Add(1);//加上第一页     
  •             if (PageCount > 1) showPagesList.Add(2);  
  •   
  •             int showFirst = PageIndex - SelectPageCount / 2 + 1;  
  •             int showLast = PageIndex + SelectPageCount / 2;  
  •             if (showFirst < 0) showLast = showLast - showFirst;  
  •             if (showLast > PageCount) showLast = PageCount;  
  •   
  •             for (int i = showFirst; i < showLast; i++)  
  •             {  
  •                 if (!showPagesList.Contains(i) && i > 0) showPagesList.Add(i);//如果里面不存在则加入显示行列  
  •             }  
  •   
  •             if (PageCount - 1 > 1 && !showPagesList.Contains(PageCount - 1)) showPagesList.Add(PageCount - 1);//加上最后第二页  
  •             if (PageCount > 1 && !showPagesList.Contains(PageCount)) showPagesList.Add(PageCount);//加上最后一页  
  •   
  •             //写入上一页  
  •             int preIndex = PageIndex > 1 ? PageIndex - 1 : 0;  
  •             //如果当前页不如首页.则显示上一页  
  •             if (preIndex > 0)  
  •             {  
  •                 output.AddAttribute(HtmlTextWriterAttribute.Class, PageIndexCss);  
  •                 output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this"__sp_TurnPage_" + preIndex + "_" + PageCount));  
  •                 output.RenderBeginTag(HtmlTextWriterTag.A);  
  •                 output.Write("上一页");  
  •                 output.RenderEndTag();  
  •             }  
  •             //显示中间页  
  •             int showedindex = 0;  
  •             foreach (int index in showPagesList)  
  •             {  
  •                 if (index - showedindex > 1)  
  •                 {  
  •                     output.RenderBeginTag(HtmlTextWriterTag.Span);  
  •                     output.Write(" ... ");  
  •                     output.RenderEndTag();  
  •                 }  
  •                 output.AddAttribute(HtmlTextWriterAttribute.Class,index==PageIndex?CurPageIndexCss:PageIndexCss);  
  •                 if (index != PageIndex) output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this"__sp_TurnPage_" + index + "_" + PageCount));  
  •                 output.RenderBeginTag(HtmlTextWriterTag.A);  
  •                 output.Write(index);  
  •                 output.RenderEndTag();  
  •   
  •                 showedindex = index;  
  •             }  
  •   
  •             //写入下一页  
  •             int nextIndex = PageIndex < PageCount ? PageIndex + 1 : 0;  
  •             //如果当前页不为最后一页.则显示下一页  
  •             if (nextIndex > 0)  
  •             {  
  •                 output.AddAttribute(HtmlTextWriterAttribute.Class, PageIndexCss);  
  •                 output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this"__sp_TurnPage_" + nextIndex + "_" + PageCount));  
  •                 output.RenderBeginTag(HtmlTextWriterTag.A);  
  •                 output.Write("下一页");  
  •                 output.RenderEndTag();  
  •             }  
  •         }         
  •   
  •         /// <summary>  
  •         /// 响应事件  
  •         /// </summary>  
  •         /// <param name="eventArgument"></param>  
  •         public void RaisePostBackEvent(string eventArgument)  
  •         {  
  •             string[] pcs = eventArgument.Split('_');  
  •             PageIndex = int.Parse(pcs[4]);//转到第几页  
  •             PageCount = int.Parse(pcs[5]);//总共有几页  
  •   
  •             if (PageIndexChangeHanlder != null)  
  •             {  
  •                 PageIndexChangeHanlder(this, PageIndex, PageCount);//触发翻页事件  
  •             }  
  •         }  
  •     }  
  •  
    源码其实例下载:
     

    相关文章:

    • 2021-07-07
    • 2021-09-11
    • 2022-12-23
    • 2022-02-19
    猜你喜欢
    • 2021-07-02
    • 2021-11-03
    • 2022-12-23
    • 2021-11-16
    • 2021-11-26
    • 2021-09-02
    相关资源
    相似解决方案