存储过程:
CREATE PROCEDURE [dbo].[ThePagerIndex]
@strTable varchar(50) = '[dbo].[zp_ReleaseJob]',
@strField varchar(50) = '*',
@intTop int = 5000,
@pageSize int = 2,
@pageIndex int = 1,
@strWhere varchar(50) = '1=1',
@strSortKey varchar(50) = 'rel_ID',
@strSortField varchar(50) = 'rel_ID desc',
@strOrderBy bit = 1,
@pageCount int OUTPUT,
@RecordCount int OUTPUT
--@UsedTime int OUTPUT
AS
SET NOCOUNT ON
Declare @sqlcount INT
Declare @timediff DATETIME
select @timediff=getdate()
Begin Tran
DECLARE @sql nvarchar(4000),@where1 varchar(300),@where2 varchar(300)
IF @strWhere is null or rtrim(@strWhere)=''
BEGIN--没有查询条件
SET @where1=' WHERE '
SET @where2=' '
END
ELSE
BEGIN--有查询条件
SET @where1=' WHERE ('+@strWhere+') AND ' --本来有条件再加上此条件
SET @where2=' WHERE ('+@strWhere+') ' --原本没有条件而加上此条件
END
--SET @sql='SELECT @intResult=COUNT(*) FROM '+@strTable+@where2
IF @intTop<=0
BEGIN
SET @sql='SELECT @sqlcount=COUNT(*) FROM (select '+@strSortKey+' from '+ @strTable + @where2 +') As tmptab'
END
ELSE
BEGIN
SET @sql='SELECT @sqlcount=COUNT(*) FROM (select top '+ cast(@intTop as varchar(300)) +' '+@strSortKey+' from '+ @strTable + @where2 +') As tmptab'
END
--print @sql
EXEC sp_executesql @sql,N'@sqlcount int OUTPUT',@sqlcount OUTPUT --计算总记录数
SELECT @pageCount=CEILING((@sqlcount+0.0)/@pageSize) --计算总页数
SELECT @RecordCount = @sqlcount --设置总记录数
IF @pageIndex=1 --第一页
BEGIN
SET @sql='SELECT TOP '+CAST(@pageSize AS varchar(300))+' '+@strField+' FROM '+@strTable+ @where2+'ORDER BY '+ @strSortField
END
Else
BEGIN
IF @strOrderBy=0
SET @sql='SELECT TOP '+CAST(@pageSize AS varchar(300))+' '+@strField+ ' FROM '+@strTable+@where1+@strSortKey+'>(SELECT MAX('+@strSortKey+') '+ ' FROM (SELECT TOP '+CAST(@pageSize*(@pageIndex-1) AS varchar(300))+' '+ @strSortKey+' FROM '+@strTable+@where2+'ORDER BY '+@strSortField+') t) ORDER BY '+@strSortField
ELSE
SET @sql='SELECT TOP '+CAST(@pageSize AS varchar(300))+' '+@strField+' FROM '+@strTable+@where1+@strSortKey+'<(SELECT MIN('+@strSortKey+') '+ ' FROM (SELECT TOP '+CAST(@pageSize*(@pageIndex-1) AS varchar(300))+' '+ @strSortKey+' FROM '+@strTable+@where2+'ORDER BY '+@strSortField+') t) ORDER BY '+@strSortField+''
END
EXEC(@sql)
--print @sql
If @@Error <> 0
Begin
RollBack Tran
Return -1
End
Else
Begin
Commit TRAN
--set @UsedTime = datediff(ms,@timediff,getdate())
--select datediff(ms,@timediff,getdate()) as 耗时
Return @sqlcount
End
GO
cs代码:
int currPage = 1;
int PageSize = 2;
public void Bind()
{
int pageCount, RecordCount;
item.DataSource = GetPage(currPage, PageSize, out pageCount, out RecordCount);
item.DataBind();
anp.RecordCount = RecordCount;
anp.CurrentPageIndex = currPage;
anp.PageSize = PageSize;
}
protected void anp_PageChanged(object sender, EventArgs e)
{
int pageCount, RecordCount;
item.DataSource = GetPage(anp.CurrentPageIndex, PageSize, out pageCount, out RecordCount);
item.DataBind();
}
public DataSet GetPage(int pageindex, int _pageSize, out int pageCount, out int RecordCount)
{
pageCount = 0;
RecordCount = 0;
string con = GetConnectionStringDao.GetConn();
SqlConnection conn = new SqlConnection(con);
SqlCommand objcmd = new SqlCommand(".ThePagerIndex", conn);
objcmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] para ={
new SqlParameter("@strTable",SqlDbType.VarChar,-1),
new SqlParameter("@strField",SqlDbType.VarChar,-1),
new SqlParameter("@pageSize",SqlDbType.Int),
new SqlParameter("@pageIndex",SqlDbType.Int),
new SqlParameter("@strSortKey",SqlDbType.VarChar,-1),
new SqlParameter("@strSortField",SqlDbType.VarChar,-1),
new SqlParameter("@strOrderBy",SqlDbType.Bit),
new SqlParameter("@pageCount",SqlDbType.Int),
new SqlParameter("@RecordCount",SqlDbType.Int),
new SqlParameter("@intTop",SqlDbType.Int,-1)
};
para[0].Value = "OK";
para[1].Value = "*";
para[2].Value = _pageSize;
para[3].Value = pageindex;
para[4].Value = "ID";
para[5].Value = "ID desc";
para[6].Value = 1;
para[7].Value = pageCount;
para[7].Direction = ParameterDirection.Output;
para[8].Value = RecordCount;
para[8].Direction = ParameterDirection.Output;
para[9].Value = -1;
objcmd.Parameters.AddRange(para);
conn.Open();
DataSet dataSet = new DataSet();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = objcmd;
sqlDA.Fill(dataSet,"zp_ReleaseJob");
return dataSet;
RecordCount = Convert.ToInt32(objcmd.Parameters["@RecordCount"].Value);
pageCount = Convert.ToInt32(objcmd.Parameters["@pageCount"].Value);
conn.Close();
conn.Dispose();
}
你的分页代码这么多 不应该? 用pagedataSource 可直接分页
你看看anp_PageChanged事件执行没有啊?
AspNetPager分页还是回发的吗,没用过,如果是回发的那这个控件也太差劲了吧。没有回发anp_PageChanged就不会执行。
你看看是不是这个问题,其他地方没发现什么问题的,调试试试~~
protected override void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
if (!IsPostBack)
{
Bind();
}
}
int currPage = 1;
int PageSize = 10;
public void Bind()
{
int pageCount,RecordCount;
anp.Visible = true;
Repeater1.DataSource = bll.GetPage(currPage, PageSize, "", out pageCount, out RecordCount);
Repeater1.DataBind();
anp.RecordCount = RecordCount;
anp.CurrentPageIndex = currPage;
anp.PageSize = PageSize;
}
protected void anp_PageChanged(object sender, EventArgs e)
{
int pageCount, RecordCount;
Repeater1.DataSource = bll.GetPage(anp.CurrentPageIndex, PageSize,"", out pageCount, out RecordCount);
Repeater1.DataBind();
}
那个存储过程有问题 我测试了一下 只能查出第一页,建议换个分页存储过程试试。