日期:2011-10-04  浏览次数:20458 次

本代码收集整理而成 已调试通过 ITFLY8
分页存储过程
//@PageSize:分页大小,PageIndex:页号,@PageCount:总页数

,@recordCount:记录数
drop procedure GetCustomDataPage
go
CREATE PROCEDURE GetCustomDataPage
@pageSize int,
@pageIndex int,
@pageCount int output,
@recordCount int output
AS
declare @SQL varchar(1000)
select @recordCount=count(*) from products
set @pageCount=ceiling(@recordCount*1.0/@pageSize)
if @pageIndex=0 or @pageCount<=1
set @SQL='select top '+str(@pageSize)+'

productID,productName,unitPrice from products order
 by productID asc'
else if @pageIndex=@pageCount-1
set @SQL='select * from (select top '+str(@recordCount-

@pagesize*@pageIndex)+'productID,productName,
unitPrice from products order by productID desc) TempTable

order by productID asc'
else
set @SQL='select top '+str(@pageSize)+' * from (select top

'+str(@recordCount-@pageSize*@pageIndex)+'
 productID,productName,unitPrice from products order by

productID desc) TempTable order by productID asc'
exec(@SQL)
GO
.aspx代码

<form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <asp:DataGrid

id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION:

absolute; TOP: 8px" runat="server"
    

AutoGenerateColumns="False" AllowPaging="True"

AllowCustomPaging="True">
     <Columns>
     

<asp:BoundColumn DataField="productID" ReadOnly="True"

HeaderText="productID"></asp:BoundColumn>
     

<asp:BoundColumn DataField="productName"

HeaderText="productName"></asp:BoundColumn>
     

<asp:BoundColumn DataField="unitPrice"

HeaderText="unitPrice"></asp:BoundColumn>
     

<asp:ButtonColumn Text="删除"

CommandName="Delete"></asp:ButtonColumn>
     

<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="

更新" CancelText="取消" EditText="编

辑"></asp:EditCommandColumn>
     </Columns>
     <PagerStyle

Mode="NumericPages"></PagerStyle>
    </asp:DataGrid></FONT>
  </form>

.aspx.cs代码

protected System.Web.UI.WebControls.DataGrid DataGrid1;
  private uint pageCount;  //总页数
  private uint recordCount;  //总记录数


  private void Page_Load(object sender,

System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!Page.IsPostBack)
   {
    BindDataGrid();
   }
  }


private DataSet GetPageData(uint pageSize, uint pageIndex)
  {
   string strConn =

System.Configuration.ConfigurationSettings.AppSettings

["ConnectionString"];