1这是存储过程.加入SQL数据库中 
  2CREATE PROCEDURE [spGet_Customers_By_Page] 
  3@CurrentPage int
  4@PageSize int
  5@TotalRecs int  output 
  6 AS 
  7-- 创建一个 temp table 以暂存所请求的 Customers 表数据. 
  8--由于Custormers表并没有Identity列来对所有记录进行顺序编号, 
  9--因此需要在临时表中增加一个Identity列. 
 10CREATE TABLE #TempTable 
 11
 12 ID int  IDENTITY  PRIMARY KEY, 
 13 CompanyName  nvarchar(40), 
 14 ContactName     nvarchar(30), 
 15 ContactTitle        nvarchar(30), 
 16 Phone                 nvarcha*(24), 
 17 Fax                      nvarchar(24), 
 18
 19--选择了Customers表的所有行(仅限于所需要的列),并将之填充入临时表. 
 20INSERT INTO #TempTable 
 21(CompanyName , ContactName, ContactTitle, Phone, Fax) 
 22SELECT CompanyName , ContactName, ContactTitle, Phone, Fax   
 23FROM  Customers 
 24--利用@CurrentPage和@PageSize变量,可以确定所选的第一条和最后一条纪录. 
 25DECLARE @FirstRec int ,@LastRec int  
 26SELECT @FirstRec = (@CurrentPage - 1*  @PageSize    //这里的firstRec关不代表当前页首条纪录的id值,而是比id值少一的最近值. 
 27SELECT @LastRec = (@CurrentPage * @PageSize +1)       //这里的LastRec关不代表当前页首条纪录的id值,而是比id值大一的最近值 
 28                                                      //为什么要写成这样,当然有它的原因,因为只有这样才不会缺失首页的首条纪录. 
 29
 30--通过select查询语句确定返回到web表单的一页数据. 
 31SELECT  CompanyName , ContactName, ContactTitle, Phone, Fax   
 32FROM #TempTable 
 33WHERE ID > @FirstRec  AND ID <  @LastRec 
 34
 35--获得Customers表的所有记录数,并将记录数 (COUNT*) 赋给@TotalRec参数值返回. 
 36SELECT @TotalRecs = COUNT(*) FROM Customers 
 37
 382后台代码: 
 39using System; 
 40using System.Collections; 
 41using System.ComponentModel; 
 42using System.Data; 
 43using System.Drawing; 
 44using System.Web; 
 45using System.Web.SessionState; 
 46using System.Web.UI; 
 47using System.Web.UI.WebControls; 
 48using System.Data.SqlClient; 
 49namespace changedpage 
 50
 51 /// <summary> 
 52 /// WebForm1 的摘要说明。 
 53 /// </summary> 

 54 public class WebForm1 : System.Web.UI.Page 
 55 
 56  protected System.Web.UI.WebControls.Label CurrentPage; 
 57  protected System.Web.UI.WebControls.LinkButton FirstPage; 
 58  protected System.Web.UI.WebControls.LinkButton PreviousPage; 
 59  protected System.Web.UI.WebControls.LinkButton NextPage; 
 60  protected System.Web.UI.WebControls.LinkButton LastPage; 
 61  protected System.Web.UI.WebControls.DataGrid myDataGrid; 
 62  protected int CurrentPageNumber; 
 63  protected System.Web.UI.WebControls.Label TotalPages; 
 64  protected SqlConnection myconnection = new SqlConnection(); 
 65  
 66  private void Page_Load(object sender, System.EventArgs e) 
 67  
 68   // 在此处放置用户代码以初始化页面 
 69    
 70   if(!Page.IsPostBack) 
 71   
 72    CurrentPageNumber=1
 73    binddata(); 
 74   }
 
 75  }
 
 76  protected void binddata() 
 77  
 78string strconn = "server=localhost;user id=sa;password=;database=Northwind " ; 
 79   myconnection.ConnectionString= strconn; 
 80   SqlCommand mycommand = new SqlCommand("spGet_Customers_By_Page",myconnection); 
 81   //设置为存储过程 
 82   mycommand.CommandType = CommandType.StoredProcedure; 
 83   mycommand.Parameters.Add(new SqlParameter("@CurrentPage",SqlDbType.Int)); 
 84   mycommand.Parameters["@CurrentPage"].Value=CurrentPageNumber; 
 85   mycommand.Parameters.Add(new SqlParameter("@PageSize",SqlDbType.Int)); 
 86   mycommand.Parameters["@PageSize"].Value=myDataGrid.PageSize; 
 87   mycommand.Parameters.Add(new SqlParameter("@TotalRecs",SqlDbType.Int)); 
 88   mycommand.Parameters["@TotalRecs"].Direction=ParameterDirection.Output; 
 89   try 
 90   
 91    myconnection.Open(); 
 92    SqlDataReader myreader = mycommand.ExecuteReader(); 
 93    myDataGrid.DataSource=myreader; 
 94    myDataGrid.DataBind(); 
 95   }
 
 96   catch( Exception ex) 
 97   
 98    Response.Write(ex.ToString()); 
 99   }
 
100   finally 
101   
102    myconnection.Close(); 
103   }
 
104   CurrentPage.Text = CurrentPageNumber.ToString(); 
105   if(!Page.IsPostBack) 
106   
107    int Total_Records = ((int) mycommand.Parameters["@TotalRecs"].Value); 
108    decimal Total_Pages = decimal.Parse(Total_Records.ToString())/myDataGrid.PageSize; 
109     
110    TotalPages.Text=(System.Math.Ceiling(double.Parse(Total_Pages.ToString()))).ToString(); 
111   }
 
112   if(CurrentPageNumber ==1
113   
114    PreviousPage.Enabled=false
115    NextPage.Enabled=true
116   }
 
117   else 
118   
119    if(CurrentPageNumber == Int32.Parse(TotalPages.Text)) 
120    
121     PreviousPage.Enabled=true
122     NextPage.Enabled=false
123    }
 
124    else 
125    
126     PreviousPage.Enabled=true
127     NextPage.Enabled=true
128    }
 
129   }
 
130  }
 
131