现在的位置: 首页 > 综合 > 正文

新手写自定义分页控件

2012年01月06日 ⁄ 综合 ⁄ 共 6701字 ⁄ 字号 评论关闭

    本人初次写自定义控件.

 

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

 

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=\

抱歉!评论已关闭.