将分页作为一个单独的控件

来源:互联网 发布:穿越火线mac-10 编辑:程序博客网 时间:2024/05/21 06:59

namespace Web.enterprise.modules
{
 using System;
 using System.Data;
 using System.Drawing;
 using System.Web;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;

 /// <summary>
 ///  Page 的摘要说明。
 /// </summary>
 public class PageList : System.Web.UI.UserControl
 {

  private const int MAX_PAGE = 5;

  private string _url;
  private int _currentPage;
  private int _pageCount;
  private int _base = 0;

  protected System.Web.UI.WebControls.Label lblNoCount;
  protected System.Web.UI.WebControls.Label lblPageNum;
  protected System.Web.UI.WebControls.Label lblList;
  protected System.Web.UI.WebControls.Label linkNext;
  protected System.Web.UI.WebControls.Label linkPervious;
  

  /// <summary>
  ///  连接的Url,不包括分页的连接
  /// </summary>
  public string Url
  {
   get
   {
    return _url;
   }
   set
   {
    _url = value;
   }
  }

  /// <summary>
  ///  当前的页
  /// </summary>
  public int CurrentPage
  {
   get
   {
    return _currentPage;
   }
   set
   {
    _currentPage = value;
   }
  }

  /// <summary>
  ///  总的页数
  /// </summary>
  public int PageCount
  {
   get
   {
    return _pageCount;
   }
   set
   {
    _pageCount = value;
   }
  }


       //初始化第1页的值为0
  public int PageBase
  {
   get
   {
    return _base;
   }
   set
   {
    if (value !=0 )
    {
     _base = 1;
    }
    else
    {
     _base = 0;
    }
   }
  }


  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面

   if ( !IsPostBack )
   {
    InitPage();
   }

  }

 

  #region 分页
  public void InitPage(  )
  {
   //总的页数
   linkPervious.Visible = false;
   linkNext.Visible = false;
   lblList.Visible = false;
   lblPageNum.Visible = false;

   if( PageCount>0 )
   {
    linkPervious.Visible=true;
    linkNext.Visible=true;
    lblList.Visible = true;

    lblPageNum.Visible = true;

    lblNoCount.Visible = false;

   
    //如果当前页已经超出了总的页数,设置为最后一页
    if( _currentPage >= _pageCount )
    {
     _currentPage = _pageCount - 1 ;
    }

    //如果当前页小于0,则设置为第一页
    if( _currentPage<_base )
    {
     _currentPage= _base;
    }

    //最后一页不显示下一页的连接
    if ( _currentPage == _pageCount -1)
    {
     linkNext.Visible= false;
    }

    //第一页不显示上一页的连接
    if ( _currentPage == _base )
    {
     linkPervious.Visible = false;
    }
   
    //初始化页导航
    string listtext="";

    //要求仅显示10个页码
    int minPage=_currentPage-MAX_PAGE;
    int maxPage=_currentPage+MAX_PAGE;
                 
    //如果最小的页数已经少于0则初始化为0
    if( minPage < _base )
    {
     minPage = _base;

     //一开始也显示10个
     maxPage = MAX_PAGE * 2;
    }

    //如果最大的页数已经超过了总页数,则初始化为总页数
    if( maxPage > _pageCount )
    {
     maxPage = _pageCount  ;


     //后来显示10个
     if(  _pageCount >=  ( MAX_PAGE * 2) )
     {
      minPage =_pageCount - ( MAX_PAGE * 2) ;
     }
     
    }

    //循环显示列表
    for( int i=minPage ; i<= maxPage - 1;i++ )
    {
     if( i == _currentPage )
     {
      if (_base==0)
      {
       listtext=listtext+" <span class='CurrentPage'>" + ( i + 1 ) + "</span>";
      }
      else
      {
       listtext=listtext+" <span class='CurrentPage'>" + i + "</span>";
      }
     }
     else
     {
      if ( _base == 0 )
      {
       listtext=listtext+" "+"<a class='PageList' href='" + SetPage(i) + "'>" +(i+1)+ "</a>";
      }
      else
      {
       listtext=listtext+" "+"<a class='PageList' href='" + SetPage(i) + "'>" +i+ "</a>";
      }
     }

    }

    lblList.Text =listtext;

    linkPervious.Text = "<a class='PageList' href='" + SetPage(_currentPage-1) + "'>上一页</a>";
    linkNext.Text = "<a class='PageList' href='" + SetPage(_currentPage+1) + "'>下一页</a>";

    lblPageNum.Text = "第" + Convert.ToString(_currentPage+1)+"/" + PageCount + "页";
   }
   else
   {
    lblNoCount.Visible = true;
   }

 

  }

  private string SetPage( int page )
  {
   string retString = _url;

   retString = _url + "CurrentPage=" + page;

   return retString;
  }

  #endregion

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  ///  设计器支持所需的方法 - 不要使用代码编辑器
  ///  修改此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}

 

--zhangww 20060816 获取企业收藏的简历 enterprise/EnterpriseResumeManage/talentstorage.aspx
--维护内容:获取获取企业收藏的简历的数据
--维护人:jiangf  维护时间:20070625
CREATE     procedure Proc_Enterprise_Get_CustomerCollection_By_CID

(
 @CID int,
 @CurrentPage int,   --当前页数,从0开始
 @PageSize int,  --每页的数量
 @RecordCount int out, --记录数
 @PageCount int out  --页数
)

as

 declare @FieldList nvarchar(4000)
 declare @where nvarchar(4000)


set @FieldList='CCID,ResumeID,CustomerCollectionTab.Lng,
 (select top 1  PerName from PerCustomerInfoTab where PerID in ( select PerID from PerCustomerResumeTab where ResumeID = CustomerCollectionTab.ResumeID)) as PerName,
 dbo.GetSexByID( (select top 1  PerSex from PerCustomerInfoTab where PerID in ( select PerID from PerCustomerResumeTab where ResumeID = CustomerCollectionTab.ResumeID)) ) as PerSex,
 dbo.GetDegreeByID( (select top 1  PerDegree from PerCustomerInfoTab where PerID in ( select PerID from PerCustomerResumeTab where ResumeID = CustomerCollectionTab.ResumeID)) ) as PerDegree,
 (select top 1 dbo.GetPosition(PurPosition) from PerCustomerPurposeTab where CustomerCollectionTab.ResumeID = PerCustomerPurposeTab.ResumeID ) as  PositionName
'
set @where = 'ResumeID is not null and status = 1 and CID = ' + cast( @CID as nvarchar(1000))

 exec CrmPageGetPagesByMoreField
 @FieldList,
 'CustomerCollectionTab', 
 @where, 
 'CCreateDateTime desc , CCID desc', 
 'CCID', 
 @CurrentPage,   
 @PageSize,    
 @RecordCount out,  
 @PageCount out
GO

 

 

private int  CurPage = 0;  //当前页码
  private string strCurPage="";   //定义一个获取当前页码的变量
  private int RecodNum,PageNum;   //定义总记录数和总页数
  private int  PageSize = 12;          //定义每页显示的站点/记录数

EnterpriseCustomerResumeDownLoadData DownLoadData = ( new EnterpriseResumeDownLoadFacade()).GetCustomerResumeDownLoad( intCompanyID,CurPage ,PageSize ,out  RecodNum ,out PageNum);

public EnterpriseCustomerResumeDownLoadData GetCustomerResumeDownLoadData( int aCID ,int nStart,int nMaxCount , out int recordPage , out int pageNum )
  {
   EnterpriseCustomerResumeDownLoadData ds = new EnterpriseCustomerResumeDownLoadData( );
   dataConn = new SqlConnection(strConn);
   dataConn.Open();
   myCommand = new SqlCommand("Proc_Enterprise_Get_CustomerResumeLoad_By_CID",dataConn);
   myCommand.CommandType = CommandType.StoredProcedure;
   try
   {
    myCommand.Parameters.Add(new SqlParameter( "@CID" , SqlDbType.NVarChar , 3000 ));
    myCommand.Parameters.Add(new SqlParameter( "@CurrentPage" , SqlDbType.Int , 4 ));
    myCommand.Parameters.Add(new SqlParameter( "@PageSize" , SqlDbType.Int , 4 ));
    myCommand.Parameters.Add(new SqlParameter( "@RecordCount" , SqlDbType.Int , 4 ));
    myCommand.Parameters.Add(new SqlParameter( "@PageCount" , SqlDbType.Int , 4 ));

    myCommand.Parameters[0].Value = aCID ;
    myCommand.Parameters[1].Value = nStart ;
    myCommand.Parameters[2].Value = nMaxCount ;
    myCommand.Parameters[3].Direction = ParameterDirection.Output ;
    myCommand.Parameters[4].Direction = ParameterDirection.Output ;
   }
   catch(Exception error)
   {
    throw new Exception( error.Message , error );
   }
   this.sqlDataAdapter.SelectCommand = myCommand ;
   try
   {
    sqlDataAdapter.Fill( ds ,"CustomerResumeDownLoadTab");
   }
   catch(Exception error)
   {
    throw new Exception( error.Message , error );
   }
   recordPage = Convert.ToInt32( myCommand.Parameters[ "@RecordCount" ].Value );
   pageNum = Convert.ToInt32( myCommand.Parameters[ "@PageCount" ].Value );
   //返回 ds
   return ds;
  }

 

分页存储过程

CREATE  proc dbo.CrmPageGetPagesByMoreField
 @FieldList nvarchar( 3000 ), --取出的列
 @TableName nvarchar( 1000 ), --表名
 @Filter  nvarchar( 3000 ), --搜索条件
 @OrderList nvarchar( 3000 ), --排序字段 , 按主键顺序排列
 @KeyName nvarchar( 200 ), --主键的字段名称
 @CurrentPage int,   --当前页,从0开始
 @PageSize int,   --每页的页数  
 @RecordCount int out,  --总的记录数
 @PageCount int out   --总的页数
as
 Declare @sqlStr nvarchar( 4000 ) --查询语句

 Declare @strGetRecordCount nvarchar(4000)--得到记录数的语句

 --如果没有指定@Filter , 用一个恒定表达式
 if @Filter = ''
 begin
  set @Filter = ' 1 > 0 '
 end
 
 set @strGetRecordCount = 'select @RecordCount = count( distinct '+ @KeyName + ') from ' + @TableName + ' where ' + @Filter
 exec sp_executesql  @strGetRecordCount ,
    N'@RecordCount int out',
    @RecordCount out

 if @PageSize = -1
 begin
  set @PageSize = @RecordCount
 end
 
 --取出总的页数
 if @RecordCount !=0
 begin
  set @PageCount = ceiling( @RecordCount*1.0 / @PageSize )
 end
 else
 begin
  set @PageCount = 0
 end

 if @PageCount != 0
 begin
  if  @CurrentPage >= @PageCount
  begin
   set @CurrentPage = @PageCount -1
  end
 end

 --如果没有指定取出多少列,则取出所有的
 if @FieldList = ''
 begin
  set @FieldList = ' * '
 end

 if @OrderList = ''
 begin
  set @OrderList = @KeyName + ' asc '
 end

 if @RecordCount > 0
 begin
  set @sqlStr = ' select top ' + Convert( nvarchar( 50 ) , @PageSize )+ @FieldList + ' from ' + @TableName
 
  set @sqlStr = @sqlStr + ' where ' + @KeyName + ' not in ' + ' ( select top ' + Convert( nvarchar( 50 ) , @PageSize * @CurrentPage ) + @KeyName + ' from ' + @TableName + ' where ' + @Filter + ' order by ' + @OrderList + ' ) and ' + @Filter + ' order by ' +  @OrderList
 end
 else
 begin
  set @sqlStr= ' select top ' +  Convert( nvarchar( 50 ) , @PageSize )+ ' '+ @FieldList + '  from ' + @TableName +  ' where ' + @Filter  + ' order by ' +  @OrderList
 end

 print @sqlStr
 exec( @sqlStr )
GO

 


CREATE   proc dbo.Proc_Flink_Get_flinks_BySearchCondition
--进行前台和后台的搜索
--default 表示默认的排序方式
--xyy
--20061219
--exec Proc_Flink_Get_flinks_BySearchCondition '1>0 ' , '( select count(*) from FlinkSiteTab x where charindex( '',903,'', '','' + PartSite + '','' ) > 0 and x.Siteid = FlinkSiteTab.Siteid ) desc', 0 , 110 , null , null
 --@ShowType int,    --0:不补充,1:进行补充
 @where    nvarchar(3000), --where条件
 @orderby  nvarchar(3000), --order by条件
 @CurrentPage int,   --当前页数,从0开始
 @PageSize int,  --每页的数量
 @RecordCount int out, --记录数
 @PageCount int out  --页数
as
 declare @FieldList nvarchar(3000) --列表
 declare @tableName nvarchar(500) --表名

 set @FieldList = '[Siteid]  ,   
  [Sitename]  ,   
  [Siteurl]  ,   
  [LogoSrc],   
  [Imgsrc]  ,   
  [Level],
  Status,
  [PartSite] , 
  dbo.GetPartSiteNameByMoreSiteID([PartSite]) as PartSiteName,
  [FlinkPosition],
  dbo.GetFlinkPosition([FlinkPosition]) as FlinkPositionName,
   ( select SiteTypeName from FlinkSiteTypeTab where SiteTypeID = FlinkSiteTab.Sitetype) as SiteTypeName ,
  case [Type ] when 0 then ''文字链接'' when 1 then ''图片链接'' when 2 then ''不限'' End as TypeName,
  case [Status] when 0 then ''未审核'' when 1 then ''已审核'' else ''未审核'' End as StatusName, 
  [Applytime]  ,    
  [Starttime],    
  [LastRanking] ,   
  [CurrentRanking],
  (LastRanking - CurrentRanking) as SubRanking,
  [Description], 
  [AlexaRank],
  [PageRank],
  
  AllInHit,  
  AllOutHit,  
  TodayInHit,
  TodayOutHit,
  YesterDayInHit,
  YesterDayOutHit,
  (cast( AllInHit as float) / cast( datediff( dy , StartTime , getDate()) + 1 as float) ) as AvgInHit , 
  (cast( AllOutHit as float) / cast( datediff( dy , StartTime , getDate()) + 1 as float) ) as AvgOutHit,
  LastInTime , 
  LastOutTime  
 '

 set @tableName = ' FlinkSiteTab '

 --得到默认的排序方式

 set @orderby = dbo.GetFlinkOrder(@orderby)


 exec CrmPageGetPagesByMoreField
 @FieldList,
 @tableName, 
 @where, 
 @orderby, 
 'Siteid', 
 @CurrentPage,   
 @PageSize,    
 @RecordCount out,  
 @PageCount out
GO

private void btnDeleteLog_Click(object sender, System.EventArgs e)
  {
   LogFacade lofInfo = new LogFacade();
   bool isleag = false;
   int Num = 0 ;
   System.Web.UI.WebControls.CheckBox chkExport;
   foreach(DataListItem listItem in dlLog.Items)
   {
    chkExport = (CheckBox)listItem.FindControl("ChkAll");
    if(chkExport.Checked)
    {
     Num++;
     int logID = Convert.ToInt32( this.dlLog.DataKeys[listItem.ItemIndex] );
     isleag = lofInfo.DeleteLog( logID );
    }
   }
   if( Num==0 )
   {
    Response.Write("<script>alert('请选择要删除的数据!')</script>");
   }
   else if( Num>=1 )
   {
    if( isleag )
    {
     Response.Write("<script>alert('成功删除数据!')</script>");
     ShowList();
    }
    else
    {
     Response.Write("<script>alert('删除失败!')</script>");
    }
   }
  }

 

private void btnAll_Click(object sender, System.EventArgs e)
  {
   System.Web.UI.WebControls.CheckBox chkExport;
   if(btnAll.Text == "全选")
   {
    foreach(DataListItem listItem in dlLog.Items)
    {
     try
     {
      chkExport = (CheckBox)listItem.FindControl("ChkAll");
      chkExport.Checked = true ;
     }
     catch(Exception error)
     {
      throw new Exception(error.Message , error);
     }
    }
    btnAll.Text = "全消";
   }
   else
   {
    foreach(DataListItem listItem in dlLog.Items)
    {
     try
     {
      chkExport = (CheckBox)listItem.FindControl("ChkAll");
      chkExport.Checked = false ;
     }
     catch(Exception error)
     {
      throw new Exception(error.Message , error);
     }
    }
    btnAll.Text = "全选";
   }
  }

 

public bool DeleteLog(int logID)
  {
   deleteCommand = new SqlCommand("ProcAdminLogDelete",dataConn);
   deleteCommand.CommandType = CommandType.StoredProcedure;
   SqlParameterCollection sqlParams = deleteCommand.Parameters;

   //向存储过程中传递参数。
   SqlParameter parameterUserID = new SqlParameter( "@ID", SqlDbType.Int,4 );
   parameterUserID.Value = logID;
   deleteCommand.Parameters.Add( parameterUserID );

   dataConn.Open( );
   int result= deleteCommand.ExecuteNonQuery( );
   if( result==0 )
   {
    return false;
   }
   else
   {
    return true;
   }
  }