完善自定义分页控件

    上一篇写了一个简单的自定义分页控件,当时写的不够完善,不能自定义控件的样式.现在完善了,望同大家共同探讨.

 

    现在已经在网上发布的分页控件特别多,而且大多都功能特别强,但是之所以选择自己写,主要是因为自己写可以根据自己的要求来设计,不用的功能就功能免了.

 

   本控件可以自定义样式,而且传入的参数和其它常用的分页控件差不多,记录总数和一页的数量,是否自定义样式,当页面数量特别多的时候,可以在分页信息栏中显示前几页和后几页.

 

   具体代码如下:

 

Code
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);   
            
样式内容#region 样式内容
            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);
            
#endregion

            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

 

 

   调用方法

 

   前台页面

 

Code
<%@ 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>

 

   后台代码:

 

Code
   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;

    }

转载于:https://www.cnblogs.com/ASPNET2008/archive/2008/03/24/1119668.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值