扩展GridView分页功能

using  System; 
using  System.Collections.Generic; 
using  System.Collections; 
using  System.ComponentModel; 
using  System.Data; 
using  System.Text; 
using  System.Web; 
using  System.Web.UI; 
using  System.Web.UI.WebControls; 
using  System.Reflection; 
using  System.Reflection.Emit; 
using  Microsoft.ApplicationBlocks.Data; 

namespace  JDGLWebControls 

[DefaultProperty(
" SelectedValue " )] 
[ToolboxData(
" <{0}:SuperGridView runat=server></{0}:SuperGridView> " )] 
public   class  SuperGridView : GridView 

#region "声明变量" 
private  LinkButton lkFirst; 
private  LinkButton lkPrev; 
private  LinkButton lkNext; 
private  LinkButton lkLast; 
private   const   string  PAGERINFORMATION  =   " 共 {0} 条 总共 {1} 页 当前 {2} 页 每页 {3} 条 "
private   const   string  PAGERINFORMATION2  =   " 总共 {1} 页 当前 {2} 页 每页 {3} 条 "
private   const   string  NOT_RECORD_FOUND  =   " <div align=center><b style=color:red;><br>没有记录</b><!--Design By genson email:genson_diy#sina.com --></div> "
#endregion  

#region "自定义属性" 

///   <summary>  
///  记录总数 
///   </summary>  
[Category( " 自定义属性 " )] 
[DefaultValue(
- 1 )] 
[Description(
" 自定义记录总数 " )] 
[Browsable(
true )] 
public   virtual   int  RecordCount 

get  

return  ViewState[ " RecordCount " ==   null   ?   - 1  : Convert.ToInt32(ViewState[ " RecordCount " ]); 

set  

ViewState[
" RecordCount " =  value; 



///   <summary>  
///  当SuperGridView数据为空的时候,显示的文本 
///   </summary>  
public   string  EmptyText 

get  

return  ViewState[ " EmptyText " ==   null   ?  NOT_RECORD_FOUND : ViewState[ " EmptyText " ].ToString(); 

set  

ViewState[
" EmptyText " =  value; 



#endregion  

#region "重写的方法" 
protected   override   void  InitializePager(GridViewRow row,  int  columnSpan, PagedDataSource pagedDataSource) 


InitializeDropDownList(row,columnSpan, pagedDataSource); 
CreatePager(row.Cells[
0 ].Controls); 
GetRecordCount(); 
CreatePagerInformation(row.Cells[
0 ].Controls); 


// base.InitializePager(row, columnSpan, pagedDataSource); 



public   override   object  DataSource 

get  

return   base .DataSource; 

set  

base .DataSource  =  value; 
if  (value  !=   null

if  (DataSource  is  DataSet) 
RecordCount 
=  ((DataSet)DataSource).Tables[ 0 ].Rows.Count; 
if  (DataSource  is  ICollection) 
RecordCount 
=  ((ICollection)DataSource).Count; 
if  (DataSource  is  DataTable) 
RecordCount 
=  ((DataTable)DataSource).Rows.Count; 
if  (DataSource  is  DataView) 
RecordCount 
=  ((DataView)DataSource).Table.Rows.Count; 






protected   override   void  Render(HtmlTextWriter writer) 

if  (HttpContext.Current  !=   null

if  (Rows.Count  ==   0
writer.Write(NOT_RECORD_FOUND); 
else  
base .Render(writer); 

else  

base .Render(writer); 



#endregion  


#region "自定义方法" 

#region "初始化分页用到的DropDownList" 
void  InitializeDropDownList(GridViewRow row,  int  columnSpan, PagedDataSource pagedDataSource) 

DropDownList drp 
=   new  DropDownList(); 
drp.AutoPostBack 
=   true
drp.SelectedIndexChanged 
+=   new  EventHandler(drp_SelectedIndexChanged); 
LiteralControl lic 
=   new  LiteralControl( " 转到 " ); 
for ( int  i = 0 ;i < pagedDataSource.PageCount;i ++

ListItem li 
=   new  ListItem((i + 1 ).ToString()  +   " " , i.ToString()); 
if  (PageIndex  ==  i) 
li.Selected 
=   true
drp.Items.Add(li); 

TableCell cell 
=   new  TableCell(); 
cell.Controls.Add(lic); 
cell.Controls.Add(drp); 
cell.ColumnSpan 
=  columnSpan; 
row.Cells.Add(cell); 




#endregion  


#endregion  


#region "创建分页控件" 

// 创建分页控件 
public   void  CreatePager(ControlCollection c) 


c.Add(
new  LiteralControl( " &nbsp;&nbsp; " )); 
lkFirst 
=   new  LinkButton(); 
lkFirst.ID 
=   " lkFirst "
lkFirst.CommandName 
=   " Page "
lkFirst.CommandArgument 
=   " First "
lkFirst.Enabled 
=   true


lkFirst.Font.Name 
=   " webdings "
lkFirst.Font.Size 
=  FontUnit.Small; 
lkFirst.ForeColor 
=   this .ForeColor; 
lkFirst.ToolTip 
=   " 转到第一页 "
lkFirst.Text 
=   " 7 "

c.Add(lkFirst); 
c.Add(
new  LiteralControl( " &nbsp;&nbsp; " )); 


lkPrev 
=   new  LinkButton(); 
lkPrev.ID 
=   " lkPrev "
lkPrev.CommandName 
=   " Page "
lkPrev.CommandArgument 
=   " Prev "
lkPrev.Font.Name 
=   " webdings "
lkPrev.Font.Size 
=  FontUnit.Small; 
lkPrev.ForeColor 
=   this .ForeColor; 
lkPrev.ToolTip 
=   " 转到前一页 "
lkPrev.Text 
=   " 3 "
lkPrev.Enabled 
=   true
if  ( this .PageIndex  ==   0 ) lkPrev.Enabled  =   false

c.Add(lkPrev); 
c.Add(
new  LiteralControl( " &nbsp;&nbsp; " )); 


lkNext 
=   new  LinkButton(); 
lkNext.ID 
=   " lkNext "
lkNext.CommandName 
=   " Page "
lkNext.CommandArgument 
=   " Next "
lkNext.Font.Name 
=   " webdings "
lkNext.Font.Size 
=  FontUnit.Small; 
lkNext.ForeColor 
=   this .ForeColor; 
lkNext.ToolTip 
=   " 转到下一页 "
lkNext.Text 
=   " 4 "
lkNext.Enabled 
=   true
if  (PageIndex  ==  PageCount  -   1 ) lkNext.Enabled  =   false

c.Add(lkNext); 
c.Add(
new  LiteralControl( " &nbsp;&nbsp; " )); 



lkLast 
=   new  LinkButton(); 
lkLast.ID 
=   " lkLast "
lkLast.CommandName 
=   " Page "
lkLast.CommandArgument 
=   " Last "
lkLast.Font.Name 
=   " webdings "
lkLast.Font.Size 
=  FontUnit.Small; 
lkLast.ForeColor 
=   this .ForeColor; 
lkLast.ToolTip 
=   " 转到最后一页 "
lkLast.Text 
=   " 8 "

c.Add(lkLast); 
c.Add(
new  LiteralControl( " &nbsp;&nbsp; " )); 







#endregion  

#region "跳页" 
public   void  GoToPage( int  pageIndex) 

GridViewPageEventArgs e 
=   new  GridViewPageEventArgs(pageIndex); 
this .PageIndex  =  pageIndex; 
OnPageIndexChanging(e); 





private   void  drp_SelectedIndexChanged( object  sender, EventArgs e) 

DropDownList drpPager 
=  (DropDownList)sender; 

GoToPage(
int .Parse(drpPager.SelectedValue)); 


#endregion  

#region "创建分页信息" 

// 格式:共 13 条 总共 1 页 当前 1 页 
public   void  CreatePagerInformation(ControlCollection c) 

LiteralControl lc0 
=   new  LiteralControl( "   " ); 
c.Add(lc0); 
LiteralControl lc 
=   new  LiteralControl(); 
if  (RecordCount  !=   - 1

lc.Text 
=   string .Format(PAGERINFORMATION, RecordCount, PageCount, PageIndex  +   1 , PageSize); 

else  

lc.Text 
=   string .Format(PAGERINFORMATION2,  null , PageCount, PageIndex  +   1 , PageSize); 


c.Add(lc); 


#endregion  


#region "反射" 

private   void  GetRecordCount() 

if  ( ! AllowPaging)  return
if  ( ! string .IsNullOrEmpty(DataSourceID)) 

try  

Control c 
=   this .NamingContainer; 
Control datasource
=  c.FindControl(DataSourceID); 
if  (datasource  is  DataSourceControl) 

DataSourceControl dsc 
=  datasource  as  DataSourceControl; 
if  (dsc  is  SqlDataSource) 

SqlDataSource sds 
=  dsc  as  SqlDataSource; 
CommandType type 
=  CommandType.StoredProcedure; 
if  (sds.SelectCommandType  ==  SqlDataSourceCommandType.Text) 

type 
=  CommandType.Text; 

DataTable dt 
=  SqlHelper.ExecuteDataset(sds.ConnectionString, type, sds.SelectCommand).Tables[ 0 ]; 
RecordCount 
=  dt.Rows.Count; 


if  (dsc  is  ObjectDataSource) 

// ObjectDataSource ods = dsc as ObjectDataSource; 
// if (ods.EnablePaging) 
//
//  throw new NotSupportedException("Super不支持ObjectDataSource属性EnablePaging=true的分页"); 
//
// else 
//
//  Type typebll= Type.GetType(ods.TypeName); 
//  typebll.InvokeMember(ods.SelectMethod, BindingFlags.DeclaredOnly | BindingFlags.Static 
//  | BindingFlags.InvokeMethod|BindingFlags.Public, null, null, new Object[] { string.Empty }); 
//   //  MethodInfo method = typebll.GetMethod(ods.SelectMethod); 



//



catch  (Exception ex) 

// throw ex; 




#endregion  




 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值