新手写自定义分页控件

    本人初次写自定义控件.

 

   下面是一个自定义分页控件,用户可以通过传入记录总数和每页显示的记录数来实现分页导航.

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace pagerControls
{
    [DefaultProperty(
"Text")]
    [ToolboxData(
"<{0}:WebCustomControlPager runat=server></{0}:WebCustomControlPager>")]
    
public class WebCustomControlPager : WebControl
    
{
         
        [Bindable(
true)]
        [CategoryAttribute(
"Appearance")]
        [DefaultValue(
"")]
        [Localizable(
true)]
        
public virtual string Text
        
{
            
get
            
{
                String s 
= (String)ViewState["Text"];
                
return ((s == null? String.Empty : s);
            }


            
set
            
{
                ViewState[
"Text"= value;
            }

        }

        
private int _currentPageIndex;
        
/// <summary>
        
/// 当前页面索引
        
/// </summary>

        [Bindable(true)]
        [CategoryAttribute(
"Appearance")]
        [Localizable(
true)]
        [DescriptionAttribute(
"当前页面索引从1开始")]
        [DefaultValueAttribute(
"当前页面索引")]
        
public virtual int currentPageIndex
        
{
            
get
            
{
                
return this._currentPageIndex;
            }


            
set
            
{
                
this._currentPageIndex = value;
            }

        }

        
private int _iRecordCount=1;
        
/// <summary>
        
/// 记录数量
        
/// </summary>

        [Bindable(true)]
        [CategoryAttribute(
"Appearance")]
        [Localizable(
true)]
        [DescriptionAttribute(
"记录数量")]
        [DefaultValueAttribute(
"记录数量")]
        
public virtual int iRecordCount
        
{
            
get
            
{
                
return this._iRecordCount;
               
            }


            
set
            
{
                
this._iRecordCount = value;
            }

        }

        
private int _iRowsCount=10;
        
/// <summary>
        
/// 每页记录数量
        
/// </summary>

        [Bindable(true)]
        [CategoryAttribute(
"Appearance")]
        [Localizable(
true)]
        [DescriptionAttribute(
"每页记录数量")]
        [DefaultValueAttribute(
"每页记录数量")]
        
public virtual int iRowsCount
        
{
            
get
            
{
                
return this._iRowsCount;
                
            }


            
set
            
{
                
this._iRowsCount = value;
            }

        }

        
private int _iPrevCount=5;
        
/// <summary>
        
/// 前部分记录数量
        
/// </summary>

        [Bindable(true)]
        [CategoryAttribute(
"Appearance")]
        [Localizable(
true)]
        [DescriptionAttribute(
"前部分记录数量")]
        [DefaultValueAttribute(
"前部分记录数量")]
        
public virtual int iPrevCount
        
{
            
get
            
{
                
return this._iPrevCount;
                
            }


            
set
            
{
                
this._iPrevCount = value;
            }

        }

        
private int _iNextCount=5;
        
/// <summary>
        
/// 后部分记录数量
        
/// </summary>

        [Bindable(true)]
        [CategoryAttribute(
"Appearance")]
        [Localizable(
true)]
        [DescriptionAttribute(
"后部分记录数量")]
        [DefaultValueAttribute(
"后部分记录数量")]
        
public virtual int iNextCount
        
{
            
get
            
{
                
                
return this._iNextCount  ;
                
            }


            
set
            
{
                
this._iNextCount = value;
            }

        }

        
protected override void RenderContents(HtmlTextWriter output)
        
{
            
//分页样式表信息
            string sStyle = "";
            StringBuilder strbStyle 
= new StringBuilder();
            sStyle 
= "<style type =\"text/css\" >";
            strbStyle.Append(sStyle);
            sStyle 
= ".a4:link,.a4:visited,.a4:active{color:#207FC3;font-size:12px;text-decoration:none;}";
            strbStyle.Append(sStyle);
            sStyle 
= ".a4:hover{color:#ff6600;font-size:12px;text-decoration:none;}";
            strbStyle.Append(sStyle);
            sStyle 
= ".a5:link,.a5:visited,.a5:active{color:#ffffff;font-size:12px;text-decoration:none;}";
            strbStyle.Append(sStyle);
            sStyle 
= ".a5:hover{color:#ffffff;font-size:12px;text-decoration:none;}";
            strbStyle.Append(sStyle);
            sStyle 
= ".survey_pagediv {float:left;width:950px;height:22px; margin-top:15px;}";
            strbStyle.Append(sStyle);
            sStyle 
= ".survey_pagediv .pagedivcenter { width:600px; margin: 0 auto;}";
            strbStyle.Append(sStyle);
            sStyle 
= ".survey_pagediv .page { float:left;width:auto;font-family: Verdana, Arial, Helvetica, sans-serif;}";
            strbStyle.Append(sStyle);
            sStyle 
= ".survey_pagediv .page .select{float:left;height:16px;line-height:16px;padding:0 4px 0 4px;display:block;border:solid 1px #207FC3;margin:0 2px 0 2px;background-color:#207FC3;}";
            strbStyle.Append(sStyle);
            sStyle 
= ".survey_pagediv .page .num{float:left;height:16px;line-height:16px;padding:0 4px 0 4px;display:block;border:solid 1px #207FC3;margin:0 2px 0 2px;}";
            strbStyle.Append(sStyle);
            sStyle 
= ".survey_pagediv .page span{float:left;height:18px;line-height:18px;display:block;margin:0 4px 0 4px;}";
            strbStyle.Append(sStyle);
            sStyle 
= ".survey_pagediv_nodiv { float:left; width:230px; height:auto; border:solid 1px #C2E8C7; background-color:#FBFFFB; padding:5px;}";
            strbStyle.Append(sStyle);
            sStyle 
= ".survey_pagediv_nodiv1 { float:left; width:230px; height:155px; border:solid 1px #FFD4E3; background-color:#FFFCFE; padding:5px; text-align:center; line-height:155px;}";
            strbStyle.Append(sStyle);
            sStyle 
= "</style>";
            strbStyle.Append(sStyle);
            
//分页信息
            string sPagerHtml = "";
            sPagerHtml 
= this.CSgetPagerHtml(this .iRecordCount , this .iRowsCount , this.Text, this .currentPageIndex );
            sPagerHtml 
= strbStyle.ToString() + sPagerHtml;
            output.Write(sPagerHtml);
        }

        
        
/// <summary>
        
/// 取得分页信息
        
/// by minjiang 08-3-11
        
/// </summary>
        
/// <param name="recordCount">记录总数</param>
        
/// <param name="iRowsCount">每页记录大小</param>
        
/// <param name="pageUrl">页面地址</param>
        
/// <returns></returns>

        public string CSgetPagerHtml(int recordCount, int iRowsCount, string pageUrl, int  iCurrentPageIndex)
        
{
            
int intPrevPageCount = this .iPrevCount ;
            
int intNextPageCount = this .iNextCount ;
            
//如果小于此数字则不显示省略号
            int intDefaultCount = 10;
            StringBuilder strb 
= new StringBuilder();
            
string info = "";
            info 
= "<div class=\"survey_pagediv\"><div class=\"pagedivcenter\"><div class=\"page\">";
            strb.Append(info);
            
//链接地址
            if (pageUrl.IndexOf("page="!= -1)
            
{
                
int pageIndex = pageUrl.IndexOf("page=");
                pageUrl 
= pageUrl.Substring(0, pageIndex + 5);



            }

            
else
            
{
                
//如果页面没有任何参数则加上?与参数
                if (pageUrl.IndexOf("?"== -1)
                
{
                    pageUrl 
+= "?page=";

                }

                
else
                
{
                    
//如果只是没有page参数则加上些参数
                    pageUrl += "&page=";

                }


            }

            
//页面总数
            int pageCount = 1;
            
if (recordCount > 0)
            
{
                
if (recordCount % iRowsCount == 0)
                
{ pageCount = recordCount / iRowsCount; }
                
else
                
{
                    pageCount 
= recordCount / iRowsCount + 1;

                }


            }


            
int currentPage = iCurrentPageIndex ;
            
//int.TryParse(sCurrentPageIndex, out currentPage);
            if (currentPage < 1)
            
{
                currentPage 
= 1;


            }

            
//上一页索引
            int prevPage = 1;
            prevPage 
= currentPage - 1;
            
if (prevPage < 1)
            
{ prevPage = 1; }
            
//下一页索引
            int nextPage = 1;
            nextPage 
= currentPage + 1;
            
if (nextPage > pageCount)
            
{ nextPage = 1; }
            
//开始的索引页
            int startPageIndex = currentPage;
            
//要统计的页面总数
            int pageTotalCount = pageCount - currentPage + 1;
            info 
= "<span><a href=\"" + pageUrl + prevPage.ToString() + "\">&lt;&lt;上一页</a></span>";
            strb.Append(info);
            
string pageClass = "num a4";
            
//当前页的样式为select a5
            
//如果页数小于等于10页则不显示省略号
            if (pageCount <= intDefaultCount)
            
{
                
for (int kk = 1; kk <= pageCount; kk++)
                
{
                    pageClass 
= "num a4";
                    
if (kk == currentPage)
                    
{
                        pageClass 
= "select a5";


                    }

                    info 
= "<a href=\"" + pageUrl + kk.ToString() + "\" class=\"" + pageClass + "\">" + kk.ToString() + "</a>";
                    strb.Append(info);

                }

                
//下一页地址
                info = "<span><a href=\"" + pageUrl + nextPage.ToString() + "\">下一页&gt;&gt;</a></span>";
                strb.Append(info);
                info 
= strb.ToString();
                
return info;

            }

            
//省略号前显示5页
            if (pageTotalCount <= intPrevPageCount)
            
{
                
for (int k = currentPage; k <= pageCount; k++)
                
{
                    pageClass 
= "num a4";
                    
if (k == currentPage)
                    
{
                        pageClass 
= "select a5";

                    }

                    info 
= "<a href=\"" + pageUrl + k.ToString() + "\" class=\"" + pageClass + "\">" + k.ToString() + "</a>";
                    strb.Append(info);

                }




            }

            
else
            
{

                
for (int k = currentPage; k <= currentPage + intPrevPageCount - 1; k++)
                
{
                    pageClass 
= "num a4";
                    
if (k == currentPage)
                    
{
                        pageClass 
= "select a5";

                    }

                    info 
= "<a href=\"" + pageUrl + k.ToString() + "\" class=\"" + pageClass + "\">" + k.ToString() + "</a>";
                    strb.Append(info);

                }


                info 
= " <span></span>";
                strb.Append(info);

                
//开始索引
                int _iNextSatrIndex = currentPage + intPrevPageCount;
                
//剩下页面个数
                int lastPages = pageCount - _iNextSatrIndex + 1;
                
if (lastPages >= intNextPageCount)
                
{
                    
for (int j = pageCount - intNextPageCount + 1; j <= pageCount; j++)
                    
{
                        pageClass 
= "num a4";
                        
if (j == currentPage)
                        
{
                            pageClass 
= "select a5";

                        }

                        info 
= "<a href=\"" + pageUrl + j.ToString() + "\" class=\"" + pageClass + "\">" + j.ToString() + "</a>";
                        strb.Append(info);


                    }


                }

                
else
                
{
                    
for (int jj = _iNextSatrIndex; jj <= pageCount; jj++)
                    
{
                        pageClass 
= "num a4";
                        
if (jj == currentPage)
                        
{
                            pageClass 
= "select a5";

                        }

                        info 
= "<a href=\"" + pageUrl + jj.ToString() + "\" class=\"" + pageClass + "\">" + jj.ToString() + "</a>";
                        strb.Append(info);

                    }


                }




            }


            info 
= "<span><a href=\"" + pageUrl + nextPage.ToString() + "\">下一页&gt;&gt;</a></span>";
            strb.Append(info);
            info 
= "</div></div></div>";
            strb.Append(info);
            info 
= strb.ToString();
            
return info;

        }

    }

}

  

    这个控件的缺陷在于不能控制文本的样式。

 

 

   上面是效果图

 

 

 

posted on 2008-03-18 16:09  min.jiang  阅读(2074)  评论(7编辑  收藏  举报