原创--DataGrid自动分页例子,通过存储过程

通过存储过程来进行DataGrid自动分页,效率很高,可以进行百万和千万级的分页

自己通过50万条记录测试,翻至任何页,需时小于一秒

呵呵,仅供自己以后察看使用,所以代码写的不很规范

所需的存储过程如下:

None.gif CREATE PROCEDURE UP_GetRecordByPage
None.gif    @tblName      varchar(
255 ),        --  表名
None.gif    @fldName      varchar(
255 ),        --  主键字段名
None.gif    @PageSize     
int   =   10 ,            --  页尺寸
None.gif    @PageIndex    
int   =   1 ,             --  页码
None.gif    @IsReCount    bit 
=   1 ,             --  返回记录总数, 非  0  值则返回
None.gif    @OrderType    bit 
=   0 ,             --  设置排序类型, 非  0  值则降序
None.gif    @strWhere     varchar(
1000 =   ''    --  查询条件 (注意: 不要加 where)
None.gifAS
None.gif
None.gifdeclare @strSQL   varchar(
6000 )        --  主语句
None.gifdeclare @strTmp   varchar(
100 )         --  临时变量
None.gifdeclare @strOrder varchar(
400 )         --  排序类型
None.gif
None.gif
if  @OrderType  !=   0
None.gifbegin
None.gif    
set  @strTmp  =   ' <(select min '
None.gif    
set  @strOrder  =   '  order by [ '   +  @fldName  + ' ] desc '
None.gifend
None.gif
else
None.gifbegin
None.gif    
set  @strTmp  =   ' >(select max '
None.gif    
set  @strOrder  =   '  order by [ '   +  @fldName  + ' ] asc '
None.gifend
None.gif
None.gif
set  @strSQL  =   ' select top  '   +  str(@PageSize)  +   '  * from [ '
None.gif    
+  @tblName  +   ' ] where [ '   +  @fldName  +   ' ] '   +  @strTmp  +   ' ([ '
None.gif    
+  @fldName  +   ' ]) from (select top  '   +  str((@PageIndex - 1 ) * @PageSize)  +   '  [ '
None.gif    
+  @fldName  +   ' ] from [ '   +  @tblName  +   ' ] '   +  @strOrder  +   ' ) as tblTmp) '
None.gif    
+  @strOrder
None.gif
None.gif
if  @strWhere  !=   ''
None.gif    
set  @strSQL  =   ' select top  '   +  str(@PageSize)  +   '  * from [ '
None.gif        
+  @tblName  +   ' ] where [ '   +  @fldName  +   ' ] '   +  @strTmp  +   ' ([ '
None.gif        
+  @fldName  +   ' ]) from (select top  '   +  str((@PageIndex - 1 ) * @PageSize)  +   '  [ '
None.gif        
+  @fldName  +   ' ] from [ '   +  @tblName  +   ' ] where  '   +  @strWhere  +   '   '
None.gif        
+  @strOrder  +   ' ) as tblTmp) and  '   +  @strWhere  +   '   '   +  @strOrder
None.gif
None.gif
if  @PageIndex  =   1
None.gifbegin
None.gif    
set  @strTmp  = ''
None.gif    
if  @strWhere  !=   ''
None.gif        
set  @strTmp  =   '  where  '   +  @strWhere
None.gif
None.gif    
set  @strSQL  =   ' select top  '   +  str(@PageSize)  +   '  * from [ '
None.gif        
+  @tblName  +   ' ] '   +  @strTmp  +   '   '   +  @strOrder
None.gifend
None.gif
None.gif
if  @IsReCount  !=   0
None.gif    
set  @strSQL  =   ' select count(*) as Total from [ '   +  @tblName  +   ' ] ' + '  where  '   +  @strWhere
None.gif
None.gifexec (@strSQL)
None.gifGO
None.gif





Windows C# 页面代码

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  _0921test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// DataGridMostDataDisplay 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class DataGridMostDataDisplay : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.Label Label2;
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid dgShowC;        
InBlock.gif        
protected System.Web.UI.HtmlControls.HtmlForm Form1;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox TextBox1;
InBlock.gif        
protected System.Web.UI.WebControls.Button btnGOTO;
InBlock.gif        
protected System.Web.UI.WebControls.ImageButton ibtnFirstPage;
InBlock.gif        
protected System.Web.UI.WebControls.ImageButton ibtnPrevousPage;
InBlock.gif        
protected System.Web.UI.WebControls.ImageButton ibtnNextPage;
InBlock.gif        
protected System.Web.UI.WebControls.ImageButton ibtnLastPage;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label1;
InBlock.gif        
protected System.Data.SqlClient.SqlConnection sqlConnection1;
InBlock.gif
InBlock.gif        
InBlock.gif        
static int Records = 0;         //记录总数
InBlock.gif
        int PageSize = 10;       //页大小
InBlock.gif
        static int PageIndex = 1;   //当前页
InBlock.gif
        static int PageCount = 0;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label3;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label4;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label5;
InBlock.gif        
string strWhere = "";
InBlock.gif        
protected System.Web.UI.WebControls.Button btnBindData;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox txtShipAddress;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox txtShipName;
InBlock.gif        
static string strWhereO = "";
InBlock.gif    
InBlock.gif
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
InBlock.gif            
this.btnBindData.Click += new System.EventHandler(this.btnBindData_Click);
InBlock.gif            
this.dgShowC.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgShowC_ItemCreated);
InBlock.gif            
this.dgShowC.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgShowC_PageIndexChanged);
InBlock.gif            
this.dgShowC.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgShowC_ItemDataBound);
InBlock.gif            
this.ibtnFirstPage.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnFirstPage_Click);
InBlock.gif            
this.ibtnPrevousPage.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnPrevousPage_Click);
InBlock.gif            
this.ibtnNextPage.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnNextPage_Click);
InBlock.gif            
this.ibtnLastPage.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnLastPage_Click);
InBlock.gif            
this.btnGOTO.Click += new System.EventHandler(this.btnGOTO_Click);
InBlock.gif            
// 
InBlock.gif            
// sqlConnection1
InBlock.gif            
// 
InBlock.gif
            this.sqlConnection1.ConnectionString = "workstation id=WANGSZ;packet size=4096;integrated security=SSPI;data source=WANGS" +
InBlock.gif                
"Z;persist security info=False;initial catalog=Northwind";
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif        
private void BindGridC()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
string connectionString = "workstation id=WANGSZ;packet size=4096;integrated security=SSPI;data source=WANGSZ;persist security info=False;initial catalog=Northwind";
InBlock.gif            
InBlock.gif            
InBlock.gif            
string sqlstr = "SELECT * FROM Test where TID < 1000";
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (SqlConnection connection = new SqlConnection(connectionString))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DataSet ds 
= new DataSet();
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        connection.Open();                    
InBlock.gif                        SqlDataAdapter Dta
= new SqlDataAdapter(sqlstr,connection);
InBlock.gif                        Dta.Fill(ds,
"ds");
InBlock.gif                        Dta.Dispose();
InBlock.gif                        
this.dgShowC.DataSource = ds;
InBlock.gif                        
this.dgShowC.VirtualItemCount=5;
InBlock.gif                        
this.dgShowC.DataBind();
InBlock.gif                        Records 
= ds.Tables[0].Rows.Count;
InBlock.gif                        
this.Label1.Text = "共有: "+Records.ToString()+" 记录";
InBlock.gif                        
this.Label2.Text = "页数: "+PageIndex+"/"+Records/20;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch(System.Data.SqlClient.SqlException ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{                
InBlock.gif                        
throw new Exception(ex.Message);
ExpandedSubBlockEnd.gif                    }
            
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(SqlException SQLexc)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Write(
"提取数据时出现错误:" + SQLexc.ToString()); 
ExpandedSubBlockEnd.gif            }
 
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif        
//分页获取数据列表
InBlock.gif
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            SqlParameter[] parameters 
= dot.gif{
InBlock.gif                                            
new SqlParameter("@tblName", SqlDbType.VarChar, 255),
InBlock.gif                                            
new SqlParameter("@fldName", SqlDbType.VarChar, 255),
InBlock.gif                                            
new SqlParameter("@PageSize", SqlDbType.Int),
InBlock.gif                                            
new SqlParameter("@PageIndex", SqlDbType.Int),
InBlock.gif                                            
new SqlParameter("@IsReCount", SqlDbType.Bit),
InBlock.gif                                            
new SqlParameter("@OrderType", SqlDbType.Bit),
InBlock.gif                                            
new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
ExpandedSubBlockEnd.gif            }
;
InBlock.gif            parameters[
0].Value = "Test";
InBlock.gif            parameters[
1].Value = "TID";
InBlock.gif            parameters[
2].Value = PageSize;
InBlock.gif            parameters[
3].Value = PageIndex;
InBlock.gif            parameters[
4].Value = 0;
InBlock.gif            parameters[
5].Value = 0;
InBlock.gif            parameters[
6].Value = strWhere;    
InBlock.gif            
return RunProcedure("UP_GetRecordByPage",parameters,"ds");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//分页获取数据列表
InBlock.gif
        public DataSet GetListC(int PageSize,int PageIndex,string strWhere)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            SqlParameter[] parameters 
= dot.gif{
InBlock.gif                                            
new SqlParameter("@tblName", SqlDbType.VarChar, 255),
InBlock.gif                                            
new SqlParameter("@fldName", SqlDbType.VarChar, 255),
InBlock.gif                                            
new SqlParameter("@PageSize", SqlDbType.Int),
InBlock.gif                                            
new SqlParameter("@PageIndex", SqlDbType.Int),
InBlock.gif                                            
new SqlParameter("@IsReCount", SqlDbType.Bit),
InBlock.gif                                            
new SqlParameter("@OrderType", SqlDbType.Bit),
InBlock.gif                                            
new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
ExpandedSubBlockEnd.gif            }
;
InBlock.gif            parameters[
0].Value = "Test";
InBlock.gif            parameters[
1].Value = "TID";
InBlock.gif            parameters[
2].Value = PageSize;
InBlock.gif            parameters[
3].Value = PageIndex;
InBlock.gif            parameters[
4].Value = 1;
InBlock.gif            parameters[
5].Value = 0;
InBlock.gif            parameters[
6].Value = strWhere;    
InBlock.gif            
return RunProcedure("UP_GetRecordByPage",parameters,"ds");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif        
// 创建 SqlCommand 对象实例(用来返回一个整数值)    
InBlock.gif
        private static SqlCommand BuildIntCommand(SqlConnection connection,string storedProcName, IDataParameter[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlCommand command 
= BuildQueryCommand(connection,storedProcName, parameters );
InBlock.gif            command.Parameters.Add( 
new SqlParameter ( "ReturnValue",
InBlock.gif                SqlDbType.Int,
4,ParameterDirection.ReturnValue,
InBlock.gif                
false,0,0,string.Empty,DataRowVersion.Default,null ));
InBlock.gif            
return command;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 执行存储过程,返回影响的行数        
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="storedProcName">存储过程名</param>
InBlock.gif        
/// <param name="parameters">存储过程参数</param>
InBlock.gif        
/// <param name="rowsAffected">影响的行数</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string connectionString = "workstation id=WANGSZ;packet size=4096;integrated security=SSPI;data source=WANGSZ;persist security info=False;initial catalog=Northwind";
InBlock.gif            
using (SqlConnection connection = new SqlConnection(connectionString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int result;
InBlock.gif                connection.Open();
InBlock.gif                SqlCommand command 
= BuildIntCommand(connection,storedProcName, parameters );
InBlock.gif                rowsAffected 
= command.ExecuteNonQuery();
InBlock.gif                result 
= (int)command.Parameters["ReturnValue"].Value;
InBlock.gif                
//Connection.Close();
InBlock.gif
                return result;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// 执行存储过程
InBlock.gif
        public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string connectionString = "workstation id=WANGSZ;packet size=4096;integrated security=SSPI;data source=WANGSZ;persist security info=False;initial catalog=Northwind";
InBlock.gif            
using (SqlConnection connection = new SqlConnection(connectionString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataSet dataSet 
= new DataSet();
InBlock.gif                connection.Open();
InBlock.gif                SqlDataAdapter sqlDA 
= new SqlDataAdapter();
InBlock.gif                sqlDA.SelectCommand 
= BuildQueryCommand(connection, storedProcName, parameters );
InBlock.gif                sqlDA.Fill( dataSet, tableName );
InBlock.gif                connection.Close();
InBlock.gif                
return dataSet;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif        
InBlock.gif        
// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
InBlock.gif
        private static SqlCommand BuildQueryCommand(SqlConnection connection,string storedProcName, IDataParameter[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            SqlCommand command 
= new SqlCommand( storedProcName, connection );
InBlock.gif            command.CommandType 
= CommandType.StoredProcedure;
InBlock.gif            
foreach (SqlParameter parameter in parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                command.Parameters.Add( parameter );
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return command;            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
//可以得到@@RowCount
InBlock.gif
        public static object GetSingle(string SQLString)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string connectionString = "workstation id=WANGSZ;packet size=4096;integrated security=SSPI;data source=WANGSZ;persist security info=False;initial catalog=Northwind";
InBlock.gif            
using (SqlConnection connection = new SqlConnection(connectionString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using(SqlCommand cmd = new SqlCommand(SQLString,connection))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        connection.Open();
InBlock.gif                        
object obj = cmd.ExecuteScalar();
InBlock.gif                        
if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{                    
InBlock.gif                            
return null;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
return obj;
ExpandedSubBlockEnd.gif                        }
                
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch(System.Data.SqlClient.SqlException e)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{                        
InBlock.gif                        connection.Close();
InBlock.gif                        
throw new Exception(e.Message);
ExpandedSubBlockEnd.gif                    }
    
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
//根据存储过程绑定
InBlock.gif
        private void BindGridStore()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            strWhere 
= " ShipName like "+"'%"+ this.txtShipName.Text +"%' and ShipAddress like "+"'%"+ this.txtShipAddress.Text +"%'";
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    DataSet ds 
= GetList(PageSize,PageIndex,strWhere);
InBlock.gif
InBlock.gif                    
this.dgShowC.DataSource=ds.Tables[0].DefaultView;
InBlock.gif                    
this.dgShowC.DataBind();
InBlock.gif                    
InBlock.gif                    
if(strWhere != strWhereO)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        DataSet dsrc 
= GetListC(PageSize,PageIndex,strWhere);
InBlock.gif                        
if(dsrc.Tables[0].Columns.Count==1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            Records 
= Convert.ToInt32(dsrc.Tables[0].Rows[0][0].ToString());                            ;
InBlock.gif                            
this.Label1.Text = "总记录数:" + Records.ToString();
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        strWhereO 
= strWhere;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    PageCount 
= Records/10;
InBlock.gif                    
this.Label2.Text = "当前页数:"+PageIndex+"/"+PageCount;
InBlock.gif                    
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch(System.Data.SqlClient.SqlException ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                
InBlock.gif                    
throw new Exception(ex.Message);
ExpandedSubBlockEnd.gif                }
                    
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(SqlException SQLexc)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Write(
"提取数据时出现错误:" + SQLexc.ToString()); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void dgShowC_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.dgShowC.CurrentPageIndex = e.NewPageIndex;
InBlock.gif            PageIndex 
= e.NewPageIndex+1;
InBlock.gif            
this.BindGridC();
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//控制四个翻页按钮的显示 
InBlock.gif
        private void ibtnVisible(bool first,bool previous,bool next,bool last)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.ibtnFirstPage.Visible=first;
InBlock.gif            
this.ibtnPrevousPage.Visible=previous;
InBlock.gif            
this.ibtnNextPage.Visible=next;
InBlock.gif            
this.ibtnLastPage.Visible=last;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void btnBindData_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//this.BindGridC();
InBlock.gif
            this.BindGridStore();
InBlock.gif            PageIndex 
= 1;
InBlock.gif            
this.ibtnVisible(false,false,true,true);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void dgShowC_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//鼠标移动到每项时颜色交替效果
InBlock.gif
            if (e.Item.ItemType!=ListItemType.Header)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                e.Item.Attributes.Add(
"OnMouseOut""this.style.backgroundColor='Transparent';this.style.color='Black'");
InBlock.gif                e.Item.Attributes.Add(
"OnMouseOver""this.style.backgroundColor='#cacee1';this.style.color='Blue'");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
//鼠标的形状为小手
InBlock.gif
            e.Item.Attributes["style"= "Cursor:hand";
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif        
private void ibtnFirstPage_Click(object sender, System.Web.UI.ImageClickEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.ibtnVisible(false,false,true,true);
InBlock.gif            PageIndex 
= 1;
InBlock.gif            
this.BindGridStore();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ibtnPrevousPage_Click(object sender, System.Web.UI.ImageClickEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            
if(PageIndex == 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ibtnVisible(false,false,true,true);
InBlock.gif                PageIndex 
= 1;
InBlock.gif                
this.BindGridStore();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ibtnVisible(true,true,true,true);
InBlock.gif                PageIndex 
= PageIndex -1;
InBlock.gif                
this.BindGridStore();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ibtnNextPage_Click(object sender, System.Web.UI.ImageClickEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            
if(PageIndex == Records/10-1)
InBlock.gif                
this.ibtnVisible(true,true,false,false);
InBlock.gif            
else
InBlock.gif                
this.ibtnVisible(true,true,true,true);
InBlock.gif            PageIndex 
= PageIndex + 1;
InBlock.gif            
this.BindGridStore();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ibtnLastPage_Click(object sender, System.Web.UI.ImageClickEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.ibtnVisible(true,true,false,false);
InBlock.gif            PageIndex 
= PageCount;
InBlock.gif            
this.BindGridStore();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void dgShowC_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(e.Item.ItemType == ListItemType.Pager)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                
foreach (Control c in e.Item.Cells[0].Controls)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (c is Label)  //当前页数
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    dot.gif{
InBlock.gif                        Label lblpage
=(Label)c;
InBlock.gif                        
//      lblpage.ForeColor= System.Drawing.ColorTranslator.FromHtml("#e78a29"); //#e78a29 ,#FF0000     
InBlock.gif                        
//      lblpage.Font.Bold=true;
InBlock.gif
                        lblpage.Text="[<font color=#e78a29><b>"+lblpage.Text+"</b></font>]";     
InBlock.gif                        
//((Label)c).ForeColor = System.Drawing.Color.Green;      
InBlock.gif                        
//      break;
ExpandedSubBlockEnd.gif
                    }

InBlock.gif                    
if(c is LinkButton) //链接的其他页数
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    dot.gif{      
InBlock.gif                        LinkButton linkButton 
= (LinkButton)c;       
InBlock.gif                        linkButton.Text 
= "[" + linkButton.Text+"]"
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }
    
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
//跳转页面
InBlock.gif
        private void btnGOTO_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            PageIndex 
= System.Convert.ToInt32(this.TextBox1.Text);
InBlock.gif            
this.BindGridStore();
InBlock.gif            
this.TextBox1.Text = "";
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btnBindData_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/Dragon-China/archive/2007/03/06/665180.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值