.Net分页控件

 

.Net分页控件

特性:
1、支持datagrid,datalist等分种数据显示控件,理论上只要有datasource的控件都可以
2、支持url方式分页,url分页的每一页都可以被搜索引擎搜到,并且支持url重写
3、控件样式可自定义
4、封装完全,使用过程序只需写一行代码

  1 /******************************************************************
  2 ** 文件名:Pager.cs
  3 ** 创建人:杨响武
  4 ** 日 期:2005年7月27日
  5 ** 修改人:
  6 ** 日 期:
  7 ** 描 述:
  8 **
  9 **----------------------------------------------------------------
 10 ******************************************************************/
 11
 12 using System;
 13 using System.Web.UI.WebControls;
 14 using System.Web.UI.HtmlControls;
 15 using System.Web;
 16 using System.Web.UI;
 17 using System.Text.RegularExpressions;
 18
 19 namespace MyCompany.Controls
 20 {
 21     ///
 22     /// 分页控件
 23     ///
 24     public class Pager : Label, INamingContainer
 25     {
 26         #region 成员变量和构造函数
 27         HttpContext context = HttpContext.Current;
 28         Label countLabel;
 29         HyperLink previousButton;
 30         HyperLink nextButton;
 31         HyperLink firstButton;
 32         HyperLink lastButton;
 33         HyperLink[] pagingLinkButtons;
 34        
 35         public override ControlCollection Controls
 36         {
 37             get
 38             {
 39                 EnsureChildControls();
 40                 return base.Controls;
 41             }
 42         }
 43
 44         protected override void CreateChildControls()
 45         {
 46             Controls.Clear();
 47
 48             AddCountLabel();
 49
 50             AddPageButtons();
 51
 52             AddPreviousNextHyperLinks();
 53
 54             AddFirstLastHyperLinks();
 55         }
 56         #endregion
 57
 58         #region 呈现方法
 59         protected override void Render(HtmlTextWriter writer)
 60         {
 61 //            修改总页数小于等于1的时候不呈现任何控件
 62 //            int totalPages = CalculateTotalPages();
 63 //
 64 //            if (totalPages <= 1)
 65 //                return;
 66
 67             AddAttributesToRender(writer);
 68
 69             writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass, false);
 70
 71             RenderCountLabel(writer);
 72
 73             RenderFirst(writer);
 74
 75             RenderPrevious(writer);
 76
 77             RenderPagingButtons(writer);
 78            
 79             RenderNext(writer);
 80
 81             RenderLast(writer);
 82         }
 83
 84         void RenderCountLabel(HtmlTextWriter writer)
 85         {
 86             countLabel.RenderControl(writer);
 87
 88             LiteralControl l = new LiteralControl(" ");
 89             l.RenderControl(writer);
 90         }
 91
 92         void RenderFirst (HtmlTextWriter writer)
 93         {
 94             firstButton.RenderControl(writer);
 95
 96             LiteralControl l = new LiteralControl(" ");
 97             l.RenderControl(writer);
 98         }
 99
100         void RenderLast (HtmlTextWriter writer)
101         {
102             lastButton.RenderControl(writer);
103
104             LiteralControl l = new LiteralControl(" ");
105             l.RenderControl(writer);
106         }
107
108         void RenderPrevious (HtmlTextWriter writer)
109         {
110             previousButton.RenderControl(writer);
111
112             LiteralControl l = new LiteralControl(" ");
113             l.RenderControl(writer);
114         }
115
116         void RenderNext(HtmlTextWriter writer)
117         {
118             nextButton.RenderControl(writer);
119
120             LiteralControl l = new LiteralControl(" ");
121             l.RenderControl(writer);
122         }
123
124         private void RenderButtonRange(int start, int end, HtmlTextWriter writer)
125         {
126             for (int i = start; i < end; i++)
127             {
128                 if (PageIndex == i)
129                 {
130                     Literal l = new Literal();
131                     l.Text = "" + (i + 1).ToString() + "";
132
133                     l.RenderControl(writer);
134                 }
135                 else
136                 {
137                     pagingLinkButtons[i].RenderControl(writer);
138                 }
139                 if (i < (end - 1))
140                     writer.Write(" ");
141             }
142
143             LiteralControl l1 = new LiteralControl(" ");
144             l1.RenderControl(writer);
145         }
146
147         private void RenderPagingButtons(HtmlTextWriter writer)
148         {
149             int totalPages;
150
151             totalPages = CalculateTotalPages();
152
153             if (totalPages <= 10)
154             {
155                 RenderButtonRange(0, totalPages, writer);
156             }
157             else
158             {
159                 int lowerBound = (PageIndex - 4);
160                 int upperBount = (PageIndex + 6);
161
162                 if (lowerBound <= 0)
163                     lowerBound = 0;
164
165                 if (PageIndex <= 4)
166                     RenderButtonRange(0, 10, writer);
167
168                 else if (PageIndex < (totalPages - 5))
169                     RenderButtonRange(lowerBound, (PageIndex + 6), writer);
170
171                 else if (PageIndex >= (totalPages - 5))
172                     RenderButtonRange((totalPages - 10), totalPages, writer);
173             }
174         }
175
176         #endregion
177
178         #region 控件树方法
179         ///
180         /// 信息标签
181         ///
182         void AddCountLabel()
183         {
184             countLabel = new Label();
185             countLabel.ID = "countLabel";
186             countLabel.Text = string.Format(text, CalculateTotalPages().ToString("n0"));
187
188             Controls.Add(countLabel);
189         }
190
191         private void AddPageButtons()
192         {
193             pagingLinkButtons = new HyperLink[CalculateTotalPages()];
194
195             for (int i = 0; i < pagingLinkButtons.Length; i++)
196             {
197                 pagingLinkButtons[i] = new HyperLink();
198                 pagingLinkButtons[i].EnableViewState = false;
199                 pagingLinkButtons[i].Text = (i + 1).ToString();
200                 pagingLinkButtons[i].ID = i.ToString();
201                 pagingLinkButtons[i].CssClass = linkCss;
202                 pagingLinkButtons[i].NavigateUrl = GetHrefString(i);
203
204                 Controls.Add(pagingLinkButtons[i]);
205             }
206         }
207
208         ///
209         /// 首页末页
210         ///
211         void AddFirstLastHyperLinks()
212         {
213             firstButton = new HyperLink();
214             firstButton.ID = "First";
215             firstButton.Text = "首页";
216             if(PageIndex != 0 && CalculateTotalPages() > 0)
217             {
218                 firstButton.NavigateUrl = GetHrefString(0);
219             }
220             else
221             {
222                 firstButton.Enabled = false;
223             }
224            
225             Controls.Add(firstButton);
226
227             lastButton = new HyperLink();
228             lastButton.ID = "Last";
229             lastButton.Text = "末页";
230             if(PageIndex != CalculateTotalPages() - 1)
231             {
232                 lastButton.NavigateUrl = GetHrefString(CalculateTotalPages() - 1);
233             }
234             else
235             {
236                 lastButton.Enabled = false;
237             }
238             Controls.Add(lastButton);
239         }
240
241         ///
242         /// 上一页下一页
243         ///
244         void AddPreviousNextHyperLinks()
245         {
246             previousButton = new HyperLink();
247             previousButton.ID = "Prev";
248             previousButton.Text = "上一页";
249             if (HasPrevious)
250             {
251                 previousButton.NavigateUrl = GetHrefString(PageIndex - 1);
252             }
253             else
254             {
255                 previousButton.Enabled = false;
256             }
257             Controls.Add(previousButton);
258
259             nextButton = new HyperLink();
260             nextButton.ID = "Next";
261             nextButton.Text = "下一页";
262             if(HasNext)
263             {
264                 nextButton.NavigateUrl = GetHrefString(PageIndex + 1);
265             }
266             else
267             {
268                 nextButton.Enabled = false;
269             }
270             Controls.Add(nextButton);
271         }
272         #endregion
273
274         #region 私有属性
275         private bool HasPrevious
276         {
277             get
278             {
279                 if (PageIndex > 0)
280                     return true;
281
282                 return false;
283             }
284         }
285
286         private bool HasNext
287         {
288             get
289             {
290                 if (PageIndex + 1 < CalculateTotalPages() )
291                     return true;
292
293                 return false;
294             }
295         }
296         #endregion
297
298         #region 帮助方法和公共属性
299        
300         ///
301         /// 获取分页导航按钮的超链接字符串
302         ///
303         /// 该分页按钮相对应的页索引
304         /// 分页导航按钮的超链接字符串
305         private string GetHrefString(int pageIndex)
306         {
307             string url = Page.Request.RawUrl;
308
309             if (Regex.IsMatch(url, @"(d+).shtml", RegexOptions.IgnoreCase))
310             {
311                 url = Regex.Replace(url, @"(d+).shtml", pageIndex + ".shtml");
312             }
313
314             if (Regex.IsMatch(url, @"PageIndex=(d+)", RegexOptions.IgnoreCase))
315             {
316                 url = Regex.Replace(url, @"PageIndex=(d+)", "PageIndex=" + pageIndex.ToString());
317             }
318
319             return Page.Server.UrlPathEncode(url);
320         }
321
322         public int CalculateTotalPages()
323         {
324             int totalPagesAvailable;
325
326             if (TotalRecords == 0)
327                 return 0;
328
329             totalPagesAvailable = TotalRecords / PageSize;
330
331             if ((TotalRecords % PageSize) > 0)
332                 totalPagesAvailable++;
333
334             return totalPagesAvailable;
335         }
336
337         ///
338         /// 当前页
339         ///
340         public int PageIndex
341         {
342             get
343             {
344                 int _pageIndex = 0;
345
346                 if ((!Page.IsPostBack) && (context.PageIndex > 0))
347                     _pageIndex = context.PageIndex;
348                 else
349                     _pageIndex = Convert.ToInt32(ViewState["PageIndex"]);
350
351                 if (_pageIndex < 0)
352                     return 0;
353                 else
354                 {
355                     if (_pageIndex > CalculateTotalPages()-1)
356                         return CalculateTotalPages()-1;
357                     else
358                         return _pageIndex;
359                 }
360             }
361             set
362             {
363                 ViewState["PageIndex"] = value;
364             }
365         }
366
367         ///
368         /// 每页记录数
369         ///
370         public int PageSize
371         {
372             get
373             {
374                 int pageSize = Convert.ToInt32(ViewState["PageSize"]);
375
376                 if (pageSize == 0)
377                 {
378                     return 10;
379                 }
380
381                 return pageSize;
382             }
383             set
384             {
385                 ViewState["PageSize"] = value;
386             }
387
388         }
389
390         ///
391         /// 总记录数
392         ///
393         public int TotalRecords
394         {
395             get
396             {
397                 return Convert.ToInt32(ViewState["TotalRecords"]);
398             }
399             set
400             {
401                 ViewState["TotalRecords"] = value;
402             }
403         }
404
405         private string text = "共{0}页";
406
407         private string linkCss = "";
408         ///
409         /// 链接样式
410         ///
411         public string LinkCss
412         {
413             get { return linkCss; }
414             set { linkCss = value;}
415         }
416
417         private string textCss = "";
418         ///
419         /// 文字样式
420         ///
421         public string TextCss
422         {
423             get { return textCss; }
424             set { textCss = value;}
425         }
426
427         public string TextFormat
428         {
429             get
430             {
431                 return text;
432             }
433             set
434             {
435                 text = value;
436             }
437         }
438
439         #endregion
440     }
441 }
442
在页面上只需要向下面这样写:
1
2
3



转载于:https://www.cnblogs.com/vibratea/archive/2009/07/24/1529841.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
再次声明 千万不要把XiaoQi.dll 跟 XiaoQi.XML 给分开 2013年3月8号 修复了很多bug 调整了源代码 添加的时候 一定要引用 不要直接复制DLL 不然没有方法注释 最强大分分页控件 华丽的分页控件 实用的分页控件 有最新最实用的方法 联系小柒 下面是 AspNetHelp 类 方法概述 ///Show()弹出对话框 ///重载Show()弹出对话框 并且跳转界面 ///ShowJavascript()执行javascript脚本 ///FileDelete()给出文件虚拟路径删除文件 ///FileSize()获取上传文件大小 ///FileUpload()文件上传 路径upload/ 自动重命名 ///FileDownload()文件下载 调用方法如 FileDownload(Response,Server,文件虚拟路径) ///FileTxtWrite()写入TXT文本 给出要创建Txt 文本的地址 ///FileTxtReader()读取TXT文本 Txt 文本的地址 返回一个数组 每一行为数组里的一个元素 ///StringEncrypt() DES字符串加密中文不可以加密 ///StringDecrypt()DES解密字符串 ///StringMD5Encrypt()MD5加密 ///StringPYChar()单个汉字返回该汉字首字母 ///StringRandom()生成指定位数的随机数 ///StringEdit()给出指定字符串长度多少位后用点显示... ///StringReplaceSpace()替换文本中的空格和换行 ///StringColor()改变字的颜色 ///StringNull()当对象是空的时候 转为空字符串 ///StringConvert()汉字转拼音 ///StringNumber()数字转换中文 ///StringRep()url参数为中文时候转换加密 ///StringRes()url参数为中文时候转换解密 ///ShowWindowForm()弹出新窗口页面 ///ShowFormModalDialog()弹出模式窗口页面 ///SqlGetMaxID()给出表名列名 int类型 查询出最大列值+1 适用于主键 ///SqlGetSingle()执行一条计算查询结果语句,返回查询结果(object)。 ///SqlExecute()执行SQL语句,返回影响的记录数 ///SqlRunStr()执行SQL语句,不返回影响的记录数 ///SqlRunReturn()执行SQL语句,并返回第一行第一列结果 ///SqlGetReader()获得SqlDataReader对象 使用完须关闭DataReader,关闭数据库连接 ///SqlGetTable()执行查询语句,返回DataTable ///SqlDataSet()执行查询语句,返回DataSet ///SqlIsCunZai()执行Sql 语句查询是否有数据 如果没有返回假 ///SqlRunProc()执行存储过程 ///DataBindDropDownListDefault()DropDownList控件绑定 带默认选择项 如"请选择" ///DataBindDropDownList()DropDownList控件绑定 不带默认选择项 ///DataBindRadioButtonList()RadioButtonList控件绑定 默认第一个选中 ///DataBindCheckBoxList()CheckBoxList控件绑定 ///ElseIpAddr()调用此方法 返回当前登录的省市IP地址 ///ElseDataTimeIng()返回本地电脑的当前日期的年月日时分秒 动态的显示 JS实现 ///ElsePageLoad()页面加载数据加载完成呈现页面 ///ElseCkeck()返回页面验证码图片 使用方法:单独创建一个页面 在首次加载事件里写 AspNetHelp.ElseCkeck(Page); 在使用验证码的页面直接写 img src="这个页面的路径" 注意验证码保存在 Session["CheckCode"]中 这里是区分大小写的 ///ElseColorRGB()由于QQ截图的取色会出现RGB的颜色 这里能转换成16进制 分页控件详情请看内置文档

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值