完善自定义分页控件

上一篇写了一个简单的自定义分页控件,当时写的不够完善,不能自定义控件的样式.
现在完善了,望同大家共同探讨.
  现在已经在网上发布的分页控件特别多,而且大多都功能特别强,但是之所以选择自己写,主要是因为自己写可
以根据自己的要求来设计,不用的功能就功能免了.
 本控件可以自定义样式,而且传入的参数和其它常用的分页控件差不多,记录总数和一页的数量,是否自定义样式,
当页面数量特别多的时候,可以在分页信息栏中显示前几页和后几页.
  具体代码如下:

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
    
{
        
private string _pageUrl="";
         
/// <summary>
         
/// 当前页面地址
         
/// </summary>

        [Bindable(true)]
        [CategoryAttribute(
"Appearance")]
        [DefaultValue(
"")]
        [Localizable(
true)]
        [DescriptionAttribute(
"当前页面地址")]
        
public virtual string pageUrl
        
{
            
get
            
{
                
return this._pageUrl;
            }


            
set
            
{
                
this ._pageUrl  = 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 _IsCustomStyle;
        
/// <summary>
        
/// 是否自定义样式
        
/// 0:不是 1:是
        
/// </summary>

        [Bindable(true)]
        [CategoryAttribute(
"Appearance")]
        [Localizable(
true)]
        [DescriptionAttribute(
"是否自定义样式")]
        [DefaultValueAttribute(
"0")]
        
public virtual int IsCustomStyle
        
{
            
get
            
{
                
return this._IsCustomStyle;
            }


            
set
            
{
                
this._IsCustomStyle = 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;
            }

        }

        
/// <summary>
        
/// 取得默认样式表信息
        
/// by minjiang 08-3-24
        
/// </summary>
        
/// <returns></returns>

        private string styleHtml()
        
{
            
//分页样式表信息
            string sStyle = "";
            StringBuilder strbStyle 
= new StringBuilder();
            sStyle 
= "<style type =\"text/css\" >";
            strbStyle.Append(sStyle);   
            
样式内容
            sStyle 
= "</style>";
            strbStyle.Append(sStyle);
            
//样式内容结尾
            sStyle = strbStyle.ToString();
            
return sStyle;
           
        
        }

        
protected override void RenderContents(HtmlTextWriter output)
        
{
            
//分页样式表信息
            string sStyle = "";
            
if (this.IsCustomStyle == 0)
            
{
                sStyle 
= this.styleHtml();
            
            }

            
//分页信息
            string sPagerHtml = "";
            sPagerHtml 
= this.CSgetPagerHtml(this .iRecordCount , this .iRowsCount , this.pageUrl , this .currentPageIndex );
            sPagerHtml 
= sStyle + 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 = this .iNextCount +this .iPrevCount ;
            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
                
{
                    
if (recordCount>iRowsCount)
                    
{
                        
//如果页面数量大于1
                        pageCount = recordCount / iRowsCount + 1;

                    }

                    
else
                    
{
                        
//不显示分页信息
                        return "";
                    
                    }

                    

                }


            }

            
if (pageCount < 1)
            
{
                
//如果只有一页则不显示分页信息
                return "";
            
            }

            
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;
            
if (currentPage != 1)
            
{
                
//如果当前页面不是首页则显示上一页
                info = "<span><a href=\"" + pageUrl + prevPage.ToString() + "\">&lt;&lt;上一页 </a></span>";
                strb.Append(info);
 
            
            }

            
            
//回首页
            info = "<span><a href=\"" + pageUrl + "1\">首页</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 <= intDefaultCount )
            
{
                
//如果要统计的页面数量小于分页前部分数量加分页后部分数量
                
//要统计的页面数量与分页前部分数量加分页后部分数量的差额
                
//分页栏总是显示分页前部分数量加分页后部分数量个页面链接
                int iDispersion = intDefaultCount - pageTotalCount;
                
for (int k = currentPage-iDispersion ; 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);

                }

                
if (pageTotalCount > (intPrevPageCount + intNextPageCount))
                
{
                    
//如果要统计的页面总数大于省略号前的记录加省略号后面的记录数量则显示 ""
                    info = " <span></span>";
                    strb.Append(info); 
                
                }


                
//开始索引
                int _iNextSatrIndex = pageCount - intNextPageCount+1;
                
for (int j = _iNextSatrIndex; 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);

                }



            }

            
//回末页
            
//info = "<span><a href=\"" + pageUrl +pageCount .ToString ()+ "\"> 末页</a></span>";
            
//strb.Append(info);
            if (currentPage != pageCount)
            
{
                
//如果不是最后一页则显示下一页
                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;

        }

    }

}


   控件中有默认的样式,在分页栏信息中,省略号前后的分页数量都是默认值5
 
调用方法
前台页面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" EnableViewState ="false"  Inherits="Default5" %>
<%@ Register Assembly="pagerControls" Namespace="pagerControls" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
    
<link href="listPager.css" rel="stylesheet" type="text/css" />
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
&nbsp;<cc1:WebCustomControlPager iNextCount ="5" iPrevCount ="5"  ID="WebCustomControlPager1" runat="server" />
      
        
        
</div>
    
</form>
</body>
</html>
后台代码:

   protected void Page_Load(object sender, EventArgs e)
    
{
        
this.WebCustomControlPager1.pageUrl  = this.Request.Url.ToString();
        
int iPage = 1;
        iPage 
= this.getiMatchId("page");
        
if (iPage < 1)
        
{ iPage = 1; }
        
this.WebCustomControlPager1.currentPageIndex = iPage;
        
this.WebCustomControlPager1.iNextCount = 5;
        
this.WebCustomControlPager1.iPrevCount = 5;
        
this.WebCustomControlPager1.iRowsCount = 10;
        
this.WebCustomControlPager1.iRecordCount = 120;
        
this.WebCustomControlPager1.IsCustomStyle = 1;

    }
你只要在解决方案中,添加一个web控件库,命名为pagerControls
然后把默认创建的类更名为:WebCustomControlPager
将文章的所有代码复制过去,就可能编译成一个控件dll
这个dll就可能加入到你的网站中了。

posted on 2009-02-11 09:33 SevenDays 阅读(816) 评论(0)  编辑  收藏 所属分类: .NET技术


只有注册用户登录后才能发表评论。


网站导航:
 
<2009年2月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜