自定义的asp.net翻页控件

自定义的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
 
 
折叠 C# 代码
  1. [DefaultProperty("Text")]  
  2.     [ToolboxData("<{0}:splitpage runat=server></{0}:splitpage>")]  
  3.     public class splitpage : WebControl,IPostBackEventHandler  
  4.     {  
  5.         private int _SelectPageCount = 10;  
  6.         [Bindable(true)]  
  7.         [Category("SelectPageCount")]  
  8.         [DefaultValue("10")]  
  9.         [Localizable(true)]  
  10.         public int SelectPageCount {  
  11.             get {  
  12.                 return _SelectPageCount;  
  13.             }  
  14.             set { _SelectPageCount = value; }  
  15.         }  
  16.   
  17.         private int _PageIndex = 1;  
  18.         [Bindable(true)]  
  19.         [Category("PageIndex")]  
  20.         [DefaultValue("10")]  
  21.         [Localizable(true)]  
  22.         public int PageIndex  
  23.         {  
  24.             get  
  25.             {  
  26.                 return _PageIndex;  
  27.             }  
  28.             set { _PageIndex = value; }  
  29.         }  
  30.   
  31.         private int _PageCount = 1;  
  32.         [Bindable(true)]  
  33.         [Category("PageCount")]  
  34.         [DefaultValue("1")]  
  35.         [Localizable(true)]  
  36.         public int PageCount  
  37.         {  
  38.             get  
  39.             {  
  40.                 return _PageCount;  
  41.             }  
  42.             set { _PageCount = value; }  
  43.         }  
  44.   
  45.         string _pageindexCss = "PageIndexCSS";  
  46.         /// <summary>  
  47.         /// 翻页的样式  
  48.         /// </summary>  
  49.         [Bindable(true)]  
  50.         [Category("PageIndexCss")]  
  51.         [DefaultValue("1")]  
  52.         [Localizable(true)]  
  53.         public string PageIndexCss  
  54.         {  
  55.             get { return _pageindexCss; }  
  56.             set { _pageindexCss = value; }  
  57.         }  
  58.         string _CurPageindexCss = "PageCurIndexCSS";  
  59.         /// <summary>  
  60.         /// 当前页的样式  
  61.         /// </summary>  
  62.         [Bindable(true)]  
  63.         [Category("CurPageIndexCss")]  
  64.         [DefaultValue("1")]  
  65.         [Localizable(true)]  
  66.         public string CurPageIndexCss  
  67.         {  
  68.             get { return _CurPageindexCss; }  
  69.             set { _CurPageindexCss = value; }  
  70.         }  
  71.   
  72.         public delegate void DelegatePageIndexChange(object sender,int pageIndex,int pageCount);  
  73.         public event DelegatePageIndexChange PageIndexChangeHanlder;  
  74.   
  75.         //重写默认的标签  
  76.         protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Div; } }       
  77.     
  78.         protected override void RenderContents(HtmlTextWriter output)  
  79.         {  
  80.             //output.AddAttribute(HtmlTextWriterAttribute.Onclick,Page.ClientScript.GetPostBackEventReference(this,"test"));  
  81.             //output.RenderBeginTag(HtmlTextWriterTag.Div);  
  82.             //output.Write("test");  
  83.             //output.RenderEndTag();  
  84.   
  85.             //首先写入翻页样式  
  86.             if (CurPageIndexCss == "PageCurIndexCSS" || PageIndexCss == "PageIndexCSS")  
  87.             {  
  88.                 output.AddAttribute(HtmlTextWriterAttribute.Type"text/css");  
  89.                 output.RenderBeginTag(HtmlTextWriterTag.Style);  
  90.                 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;" +  
  91.         "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" +  
  92.     "{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;" +  
  93.        "padding-left:4px;padding-right:4px;border: 1px solid #2452ac;background-color: #2452ac;text-decoration: none;color: #fff;margin-right:6px;}");  
  94.                 output.RenderEndTag();  
  95.             }  
  96.             var showPagesList = new List<int>();  
  97.             showPagesList.Add(1);//加上第一页     
  98.             if (PageCount > 1) showPagesList.Add(2);  
  99.   
  100.             int showFirst = PageIndex - SelectPageCount / 2 + 1;  
  101.             int showLast = PageIndex + SelectPageCount / 2;  
  102.             if (showFirst < 0) showLast = showLast - showFirst;  
  103.             if (showLast > PageCount) showLast = PageCount;  
  104.   
  105.             for (int i = showFirst; i < showLast; i++)  
  106.             {  
  107.                 if (!showPagesList.Contains(i) && i > 0) showPagesList.Add(i);//如果里面不存在则加入显示行列  
  108.             }  
  109.   
  110.             if (PageCount - 1 > 1 && !showPagesList.Contains(PageCount - 1)) showPagesList.Add(PageCount - 1);//加上最后第二页  
  111.             if (PageCount > 1 && !showPagesList.Contains(PageCount)) showPagesList.Add(PageCount);//加上最后一页  
  112.   
  113.             //写入上一页  
  114.             int preIndex = PageIndex > 1 ? PageIndex - 1 : 0;  
  115.             //如果当前页不如首页.则显示上一页  
  116.             if (preIndex > 0)  
  117.             {  
  118.                 output.AddAttribute(HtmlTextWriterAttribute.Class, PageIndexCss);  
  119.                 output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this"__sp_TurnPage_" + preIndex + "_" + PageCount));  
  120.                 output.RenderBeginTag(HtmlTextWriterTag.A);  
  121.                 output.Write("上一页");  
  122.                 output.RenderEndTag();  
  123.             }  
  124.             //显示中间页  
  125.             int showedindex = 0;  
  126.             foreach (int index in showPagesList)  
  127.             {  
  128.                 if (index - showedindex > 1)  
  129.                 {  
  130.                     output.RenderBeginTag(HtmlTextWriterTag.Span);  
  131.                     output.Write(" ... ");  
  132.                     output.RenderEndTag();  
  133.                 }  
  134.                 output.AddAttribute(HtmlTextWriterAttribute.Class,index==PageIndex?CurPageIndexCss:PageIndexCss);  
  135.                 if (index != PageIndex) output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this"__sp_TurnPage_" + index + "_" + PageCount));  
  136.                 output.RenderBeginTag(HtmlTextWriterTag.A);  
  137.                 output.Write(index);  
  138.                 output.RenderEndTag();  
  139.   
  140.                 showedindex = index;  
  141.             }  
  142.   
  143.             //写入下一页  
  144.             int nextIndex = PageIndex < PageCount ? PageIndex + 1 : 0;  
  145.             //如果当前页不为最后一页.则显示下一页  
  146.             if (nextIndex > 0)  
  147.             {  
  148.                 output.AddAttribute(HtmlTextWriterAttribute.Class, PageIndexCss);  
  149.                 output.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackEventReference(this"__sp_TurnPage_" + nextIndex + "_" + PageCount));  
  150.                 output.RenderBeginTag(HtmlTextWriterTag.A);  
  151.                 output.Write("下一页");  
  152.                 output.RenderEndTag();  
  153.             }  
  154.         }         
  155.   
  156.         /// <summary>  
  157.         /// 响应事件  
  158.         /// </summary>  
  159.         /// <param name="eventArgument"></param>  
  160.         public void RaisePostBackEvent(string eventArgument)  
  161.         {  
  162.             string[] pcs = eventArgument.Split('_');  
  163.             PageIndex = int.Parse(pcs[4]);//转到第几页  
  164.             PageCount = int.Parse(pcs[5]);//总共有几页  
  165.   
  166.             if (PageIndexChangeHanlder != null)  
  167.             {  
  168.                 PageIndexChangeHanlder(this, PageIndex, PageCount);//触发翻页事件  
  169.             }  
  170.         }  
  171.     }  
 
源码其实例下载:
 

你可能感兴趣的:(asp.net)