分页控件1

 1using System;
  2using System.Web.UI;
  3using System.Web.UI.WebControls;
  4using System.ComponentModel;
  5using System.Drawing;
  6
  7namespace DataPage
  8{
  9    public enum Position{ Front, Back}
 10    /// <summary>
 11    /// WebDataPage 的摘要说明。
 12    /// </summary>

 13    [DefaultProperty("Text"), 
 14        ToolboxData("<{0}:WebDataPage runat=server></{0}:WebDataPage>")]
 15    public class WebDataPage : System.Web.UI.WebControls.WebControl,INamingContainer
 16    {
 17        #region Function
 18
 19        #region GetInt
 20        private int GetInt(object obj)
 21        {
 22            int iReturn = 0;
 23            if( obj != null )
 24            {
 25                iReturn = (int)obj;
 26            }

 27            return iReturn;
 28        }

 29        #endregion

 30
 31        #region GetString
 32        private string GetString(string strValue)
 33        {
 34            return strValue;
 35        }

 36        private string GetString(object obj)
 37        {
 38            if( obj == null )
 39            {
 40                return "";
 41            }

 42            else
 43            {
 44                return obj.ToString();
 45            }

 46        }

 47        #endregion

 48
 49        #region GetBoolean
 50        private bool GetBoolean(object obj)
 51        {
 52            if( obj != null )
 53            {
 54                return (bool)obj;
 55            }

 56            else
 57            {
 58                return true;
 59            }

 60        }

 61        #endregion

 62
 63        #region SetDropDownListValue
 64        private void SetDropDownListValue(DropDownList ddl, int iValue)
 65        {
 66            if( ddl != null )
 67            {
 68                for(int i=1; i<= iValue; i++)
 69                {
 70                    ListItem lst = new ListItem();
 71                    lst.Value = i.ToString();
 72                    lst.Text = "" +  i.ToString() + "";
 73                    ddl.Items.Add( lst );
 74                }

 75            }

 76        }

 77        #endregion

 78
 79        #endregion

 80
 81        #region 页码
 82
 83        #region PageCount 获取或设置总页数
 84        /// <summary>
 85        /// 获取或设置总页数
 86        /// </summary>

 87        public int PageCount
 88        {
 89            get
 90            {
 91                if( PageSize == 0)
 92                {
 93                    ViewState[ "PageCount" ] = 0;
 94                }

 95                else
 96                {
 97                    if( RecordCount % PageSize > 0 )
 98                    {
 99                        ViewState[ "PageCount" ] = RecordCount / PageSize + 1;
100                    }

101                    else
102                    {
103                        ViewState[ "PageCount" ] = RecordCount / PageSize;
104                    }

105                }
    
106                return GetInt( ViewState[ "PageCount" ] );
107            }

108            set
109            
110                ViewState[ "PageCount" ] = value; 
111            }

112        }

113        #endregion

114
115        #region RecordCount 获取或设置记录总数
116        /// <summary>
117        /// 获取或设置记录总数
118        /// </summary>

119        public int RecordCount
120        {
121            get
122            {
123                return GetInt( ViewState[ "RecordCount" ] );
124            }

125            set
126            
127                ViewState[ "RecordCount" ] = value; 
128            }

129        }

130        #endregion

131
132        #region PageSize 设置每页显示的记录数
133        /// <summary>
134        /// 设置每页显示的记录数
135        /// </summary>

136        public int PageSize
137        {
138            get
139            {
140                return GetInt( ViewState[ "PageSize" ] );
141            }

142            set
143            
144                ViewState[ "PageSize" ] = value; 
145            }

146        }

147        #endregion

148
149        #region CurrentPageIndex 获取或设置当前页
150        /// <summary>
151        /// 获取或设置当前页
152        /// </summary>

153        public int CurrentPageIndex
154        {
155            get
156            {
157                return GetInt( ViewState[ "CurrentPageIndex" ] );
158            }

159            set
160            
161                ViewState[ "CurrentPageIndex" ] = value; 
162            }

163        }

164        #endregion

165
166        #endregion

167
168        #region 信息是否显示
169
170        #region RecordCountIsVisible 总共N条记录是否显示
171        /// <summary>
172        /// 总共N条记录是否显示
173        /// </summary>

174        public bool RecordCountIsVisible
175        {
176            get
177            {
178                return GetBoolean( ViewState[ "RecordCountIsVisible" ] ); 
179            }

180            set
181            
182                ViewState[ "RecordCountIsVisible" ] = value; 
183            }

184        }

185        #endregion

186
187        #region PageCountIsVisible 共N页是否显示
188        /// <summary>
189        /// 共N页是否显示
190        /// </summary>

191        public bool PageCountIsVisible
192        {
193            get
194            {
195                return GetBoolean( ViewState[ "PageCountIsVisible" ] ); 
196            }

197            set
198            
199                ViewState[ "PageCountIsVisible" ] = value; 
200            }

201        }

202        #endregion

203
204        #region CurrentPageIndexIsVisible 第N页是否显示
205        /// <summary>
206        /// 第N页是否显示
207        /// </summary>

208        public bool CurrentPageIndexIsVisible
209        {
210            get
211            {
212                return GetBoolean( ViewState[ "CurrentPageIndexIsVisible" ] ); 
213            }

214            set
215            
216                ViewState[ "CurrentPageIndexIsVisible" ] = value; 
217            }

218        }

219        #endregion

220
221        #region FirstPageIsVisible 首页是否显示
222        /// <summary>
223        /// 首页是否显示
224        /// </summary>

225        public bool FirstPageIsVisible
226        {
227            get
228            {
229                return GetBoolean( ViewState[ "FirstPageIsVisible" ] ); 
230            }

231            set
232            
233                ViewState[ "FirstPageIsVisible" ] = value; 
234            }

235        }

236        #endregion

237
238        #region PrevPageIsVisible 上一页是否显示
239        /// <summary>
240        /// 上一页是否显示
241        /// </summary>

242        public bool PrevPageIsVisible
243        {
244            get
245            {
246                return GetBoolean( ViewState[ "PrevPageIsVisible" ] ); 
247            }

248            set
249            
250                ViewState[ "PrevPageIsVisible" ] = value; 
251            }

252        }

253        #endregion

254
255        #region NextPageIsVisible 下一页是否显示
256        /// <summary>
257        /// 下一页是否显示
258        /// </summary>

259        public bool NextPageIsVisible
260        {
261            get
262            {
263                return GetBoolean( ViewState[ "NextPageIsVisible" ] ); 
264            }

265            set
266            
267                ViewState[ "NextPageIsVisible" ] = value; 
268            }

269        }

270        #endregion

271
272        #region LastPageIsVisible 最后一页是否显示
273        /// <summary>
274        /// 最后一页是否显示
275        /// </summary>

276        public bool LastPageIsVisible
277        {
278            get
279            {
280                return GetBoolean( ViewState[ "LastPageIsVisible" ] ); 
281            }

282            set
283            
284                ViewState[ "LastPageIsVisible" ] = value; 
285            }

286        }

287        #endregion

288
289        #region SelectPageIsVisible 下拉列表框是否显示
290        /// <summary>
291        /// 下拉列表框是否显示
292        /// </summary>

293        public bool SelectPageIsVisible
294        {
295            get
296            {
297                return GetBoolean( ViewState[ "SelectPageIsVisible" ] ); 
298            }

299            set
300            
301                ViewState[ "SelectPageIsVisible" ] = value; 
302            }

303        }

304        #endregion

305
306        #endregion

307
308        #region 文本内容
309
310        #region FirstPageText 首页文本内容
311        /// <summary>
312        /// 首页文本内容
313        /// </summary>

314        public string FirstPageText
315        {
316            get
317            {
318                return GetString( ViewState[ "FirstPageText" ] ); 
319            }

320            set
321            
322                ViewState[ "FirstPageText" ] = value; 
323            }

324        }

325        #endregion

326
327        #region PrevPageText 上一页文本内容
328        /// <summary>
329        /// 上一页文本内容
330        /// </summary>

331        public string PrevPageText
332        {
333            get
334            {
335                return GetString( ViewState[ "PrevPageText" ] ); 
336            }

337            set
338            
339                ViewState[ "PrevPageText" ] = value; 
340            }

341        }

342        #endregion

343
344        #region NextPageText 下一页文本内容
345        /// <summary>
346        /// 下一页文本内容
347        /// </summary>

348        public string NextPageText
349        {
350            get
351            {
352                return GetString( ViewState[ "NextPageText" ] ); 
353            }

354            set
355            
356                ViewState[ "NextPageText" ] = value; 
357            }

358        }

359        #endregion

360
361        #region LastPageText 最后一页文本内容
362        /// <summary>
363        /// 最后一页文本内容
364        /// </summary>

365        public string LastPageText
366        {
367            get
368            {
369                return GetString( ViewState[ "LastPageText" ] ); 
370            }

371            set
372            
373                ViewState[ "LastPageText" ] = value; 
374            }

375        }

376        #endregion

377
378        #endregion

379
380        #region 获取或设置下拉列表框的位置
381        public Position SelectPosition
382        {
383            get
384            {
385                if( ViewState["SelectPosition"== null )
386                {
387                    return Position.Back;
388                }

389                else
390                {
391                    return (Position)ViewState["SelectPosition"];
392                }

393            }

394            set
395            {
396                ViewState["SelectPosition"= value;
397            }

398        }

399        #endregion

400        
401
402        #region CreateChildControls
403        protected override void CreateChildControls()
404        {
405            this.Controls.Clear();
406            //Tanle
407            Table tb = new Table();
408            tb.ID = "tb";
409            this.Controls.Add( tb );
410            tb.Width = Unit.Percentage(100);
411            
412            //tr
413            TableRow row = new TableRow();
414            row.ID = "row";
415            tb.Rows.Add( row );
416
417            #region 总共N条记录
418            TableCell cellRecordCount = new TableCell();
419            cellRecordCount.ID = "cellRecordCount";
420            row.Cells.Add( cellRecordCount );
421            cellRecordCount.VerticalAlign = VerticalAlign.Middle;
422            cellRecordCount.HorizontalAlign = HorizontalAlign.Center;
423            if( RecordCountIsVisible )
424            {
425                LiteralControl lcRecordCount = new LiteralControl();
426                lcRecordCount.ID = "lcRecordCount";
427                cellRecordCount.Controls.Add( lcRecordCount );
428                lcRecordCount.Text = "总共&nbsp;" + RecordCount.ToString() + "&nbsp;条记录";
429            }

430            #endregion

431
432            #region 共N页
433            TableCell cellPageCount = new TableCell();
434            cellPageCount.ID = "cellPageCount";
435            row.Cells.Add( cellPageCount );
436            cellPageCount.VerticalAlign = VerticalAlign.Middle;
437            cellPageCount.HorizontalAlign = HorizontalAlign.Center;
438            if( PageCountIsVisible )
439            {
440                LiteralControl lcPageCount = new LiteralControl();
441                lcPageCount.ID = "lcPageCount";
442                cellPageCount.Controls.Add( lcPageCount );
443                lcPageCount.Text = "共&nbsp;" + PageCount.ToString() + "&nbsp;页";
444            }

445            #endregion

446
447            #region 第N页
448            TableCell cellCurrentPageIndex = new TableCell();
449            cellCurrentPageIndex.ID = "cellCurrentPageIndex";
450            row.Cells.Add( cellCurrentPageIndex );
451            cellCurrentPageIndex.VerticalAlign = VerticalAlign.Middle;
452            cellCurrentPageIndex.HorizontalAlign = HorizontalAlign.Center;
453            if( CurrentPageIndexIsVisible )
454            {
455                LiteralControl lcCurrentPageIndex = new LiteralControl();
456                lcCurrentPageIndex.ID = "lcCurrentPageIndex";
457                cellCurrentPageIndex.Controls.Add( lcCurrentPageIndex );
458                lcCurrentPageIndex.Text = "第&nbsp;" + CurrentPageIndex.ToString() + "&nbsp;页";
459            }

460            #endregion

461
462            #region 首页
463            TableCell cellFirstPage = new TableCell();
464            cellFirstPage.ID = "cellFirstPage";
465            row.Cells.Add( cellFirstPage );
466            cellFirstPage.VerticalAlign = VerticalAlign.Middle;
467            cellFirstPage.HorizontalAlign = HorizontalAlign.Center;
468            if( FirstPageIsVisible )
469            {
470                LinkButton lbtnFirstPage = new LinkButton();
471                lbtnFirstPage.ID= "lbtnFirstPage";
472                cellFirstPage.Controls.Add( lbtnFirstPage );
473                lbtnFirstPage.Click +=new EventHandler(lbtnFirstPage_Click);
474                if( FirstPageText.Length > 0 )
475                {
476                    lbtnFirstPage.Text = FirstPageText;
477                }

478                else
479                {
480                    lbtnFirstPage.Text = "首页";
481                }

482            }

483            #endregion

484
485            #region 上一页
486            TableCell cellPrevPage = new TableCell();
487            cellPrevPage.ID = "cellPrevPage";
488            row.Cells.Add( cellPrevPage );
489            cellPrevPage.VerticalAlign = VerticalAlign.Middle;
490            cellPrevPage.HorizontalAlign = HorizontalAlign.Center;
491            if( PrevPageIsVisible )
492            {
493                LinkButton lbtnPrevPage= new LinkButton();
494                lbtnPrevPage.ID = "lbtnPrevPage";
495                cellPrevPage.Controls.Add( lbtnPrevPage );
496                lbtnPrevPage.Click +=new EventHandler(lbtnPrevPage_Click);
497                if( PrevPageText.Length > 0 )
498                {
499                    lbtnPrevPage.Text = PrevPageText;
500                }

501                else
502                {
503                    lbtnPrevPage.Text = "上一页";
504                }

505            }

506            #endregion

507            
508            #region 下一页
509            TableCell cellNextPage = new TableCell();
510            cellNextPage.ID = "cellNextPage";
511            row.Cells.Add( cellNextPage );
512            cellNextPage.VerticalAlign = VerticalAlign.Middle;
513            cellNextPage.HorizontalAlign = HorizontalAlign.Center;
514            if( NextPageIsVisible )
515            {
516                LinkButton lbtnNextPage = new LinkButton();
517                lbtnNextPage.ID= "lbtnNextPage";
518                cellNextPage.Controls.Add( lbtnNextPage );
519                lbtnNextPage.Click +=new EventHandler(lbtnNextPage_Click);
520                if( NextPageText.Length > 0 )
521                {
522                    lbtnNextPage.Text = NextPageText;
523                }

524                else
525                {
526                    lbtnNextPage.Text = "下一页";
527                }

528            }

529            #endregion

530
531            #region 最后一页
532            TableCell cellLastPage = new TableCell();
533            cellLastPage.ID = "cellLastPage";
534            row.Cells.Add( cellLastPage );
535            cellLastPage.VerticalAlign = VerticalAlign.Middle;
536            cellLastPage.HorizontalAlign = HorizontalAlign.Center;
537            if( LastPageIsVisible )
538            {
539                LinkButton lbtnLastPage = new LinkButton();
540                lbtnLastPage.ID = "lbtnLastPage";
541                cellLastPage.Controls.Add( lbtnLastPage );
542                lbtnLastPage.Click +=new EventHandler(lbtnLastPage_Click);
543                if( LastPageText.Length > 0 )
544                {
545                    lbtnLastPage.Text = LastPageText;
546                }

547                else
548                {
549                    lbtnLastPage.Text = "最后一页";
550                }

551            }

552            #endregion

553
554            #region 选择页码
555            if( SelectPageIsVisible )
556            {
557                TableCell cellSelectPage = new TableCell();
558                cellSelectPage.ID = "cellSelectPage";
559                if( SelectPosition == Position.Front )
560                {
561                    row.Cells.AddAt(0, cellSelectPage);
562                }

563                else
564                {
565                    row.Cells.Add( cellSelectPage );
566                }

567                cellSelectPage.VerticalAlign = VerticalAlign.Middle;
568                cellSelectPage.HorizontalAlign = HorizontalAlign.Center;
569
570                DropDownList ddlSelectPage = new DropDownList();
571                ddlSelectPage.ID = "ddlSelectPage";
572                cellSelectPage.Controls.Add( ddlSelectPage );
573                SetDropDownListValue( ddlSelectPage, PageCount );
574                ddlSelectPage.SelectedIndex = CurrentPageIndex - 1;
575                ddlSelectPage.AutoPostBack = true;
576                ddlSelectPage.SelectedIndexChanged +=new EventHandler(ddlSelectPage_SelectedIndexChanged);
577            }

578            
579            #endregion

580
581            base.CreateChildControls ();
582        }

583        #endregion

584
585        #region Change Pageindex
586
587        #region 首页
588        private void lbtnFirstPage_Click(object sender, EventArgs e)
589        {
590            CurrentPageIndex = 1;
591            CreateChildControls();
592            SendPageChanged();
593        }

594        #endregion

595
596        #region 上一页
597        private void lbtnPrevPage_Click(object sender, EventArgs e)
598        {
599            if( CurrentPageIndex > 1)
600            {
601                CurrentPageIndex = CurrentPageIndex - 1;
602            }

603            CreateChildControls();
604            SendPageChanged();
605        }

606        #endregion

607
608        #region 下一页
609        private void lbtnNextPage_Click(object sender, EventArgs e)
610        {
611            if( CurrentPageIndex < PageCount )
612            {
613                CurrentPageIndex = CurrentPageIndex + 1;
614            }

615            CreateChildControls();
616            SendPageChanged();
617        }

618        #endregion

619
620        #region 最后一页
621        private void lbtnLastPage_Click(object sender, EventArgs e)
622        {
623            CurrentPageIndex = PageCount;
624            CreateChildControls();
625            SendPageChanged();
626        }

627        #endregion

628
629        #region 选择页码
630        private void ddlSelectPage_SelectedIndexChanged(object sender, EventArgs e)
631        {
632            DropDownList ddlSelectPage = sender as DropDownList;
633            if( ddlSelectPage != null )
634            {
635                CurrentPageIndex = Convert.ToInt32( ddlSelectPage.SelectedValue );
636                CreateChildControls();
637                SendPageChanged();
638            }

639        }

640        #endregion

641
642        #endregion

643    
644        #region Event PageChanged
645        public event EventHandler PageChanged;
646        #endregion

647
648        #region SendPageChange
649        private void SendPageChanged()
650        {
651            if(PageChanged != null)
652            {
653                PageChanged(this,EventArgs.Empty);
654            }

655        }

656        #endregion

657
658
659    }

660}

661

 

 

 

使用方法:

<% @ Register TagPrefix="cc1" Namespace="DataPage" Assembly="DataPage"  %>
< cc1:WebDataPage  id ="WebDataPage1"  runat ="server" ></ cc1:WebDataPage >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值