**:
//總記錄數
private int totalcountrecord;
//每頁顯示的條數
private int pageitem = 5;
//當前頁
private int currentpage = 1;
private string strurl = string.empty;
protected void page_load(object sender, eventargs e)
}if (!this.ispostback)
}public void binddata()
and newstitle like '%%'", newskindid, key);
strurl = string.format("&key=&kid=", key,newskindid);
}else
",newskindid);
strurl = string.format("&kid=",newskindid);}}
else
%'",key);
strurl = string.format("&key=", key);}}
this.gridnews.datasource = news.getallnewsbystrwhere(pageitem, currentpage, strwhere, out totalcountrecord);
this.gridnews.databind();
}public void buildpagers()
else
if (currentpage + step > pagecount)
else
stringbuilder output = new stringbuilder();
");}
this.pageinfo.text = output.tostring();
this.labcount.text = string.format("總記錄數: 總頁數:", totalcountrecord, pagecount);
} 儲存過程:
create procedure usp_paginglarge
@tablenames varchar(300), --表名,可以是多個表,但不能用別名
@primarykey varchar(100), --主鍵,可以為空,但@order為空時該值不能為空
@fields varchar(350), --要取出的字段,可以是多個表的字段,可以為空,為空表示select *
@pagesize int, --每頁記錄數
@currentpage int, --當前頁,0表示第1頁
@filter varchar(200) = '', --條件,可以為空,不用填 where
@group varchar(200) = '', --分組依據,可以為空,不用填 group by
@order varchar(200) = '', --排序,可以為空,為空缺省按主鍵公升序排列,不用填 order by
@recordcount int = 0 output
asbegin
declare @sortcolumn varchar(200)
declare @operator char(2)
declare @sorttable varchar(200)
declare @sortname varchar(200)
declare @tmpselect nvarchar(200)
if @fields = ''
set @fields = '*'
if @filter = ''
set @filter = 'where 1=1'
else
set @filter = 'where ' + @filter
if @group <>''
set @group = 'group by ' + @group
if @order <> ''
begin
declare @pos1 int, @pos2 int
set @order = replace(replace(@order, ' asc', ' asc'), ' desc', ' desc')
if charindex(' desc', @order) > 0
if charindex(' asc', @order) > 0
begin
if charindex(' desc', @order) < charindex(' asc', @order)
set @operator = '<='
else
set @operator = '>='
endelse
set @operator = '<='
else
set @operator = '>='
set @sortcolumn = replace(replace(replace(@order, ' asc', ''), ' desc', ''), ' ', '')
set @pos1 = charindex(',', @sortcolumn)
if @pos1 > 0
set @sortcolumn = substring(@sortcolumn, 1, @pos1-1)
set @pos2 = charindex('.', @sortcolumn)
if @pos2 > 0
begin
set @sorttable = substring(@sortcolumn, 1, @pos2-1)
if @pos1 > 0
set @sortname = substring(@sortcolumn, @pos2+1, @pos1-@pos2-1)
else
set @sortname = substring(@sortcolumn, @pos2+1, len(@sortcolumn)-@pos2)
endelse
begin
set @sorttable = @tablenames
set @sortname = @sortcolumn
endend
else
begin
set @sortcolumn = @primarykey
set @sorttable = @tablenames
set @sortname = @sortcolumn
set @order = @sortcolumn
set @operator = '>='
enddeclare @type varchar(50)
declare @prec int
select @type=t.name, @prec=c.prec
from sysobjects o
join syscolumns c on o.id=c.id
join systypes t on c.xusertype=t.xusertype
where o.name = @sorttable and c.name = @sortname
if charindex('char', @type) > 0
set @type = @type + '(' + cast(@prec as varchar) + ')'
declare @toprows int
set @toprows = @pagesize * @currentpage + 1
print @toprows
print @operator
exec('
declare @sortcolumnbegin ' + @type + '
set rowcount ' + @toprows + '
select @sortcolumnbegin=' + @sortcolumn + ' from ' + @tablenames + ' ' + @filter + ' ' + @group + ' order by ' + @order + '
set rowcount ' + @pagesize + '
select ' + @fields + ' from ' + @tablenames + ' ' + @filter + ' and ' + @sortcolumn + '' + @operator + '@sortcolumnbegin ' + @group + ' order by ' + @order + '
')declare @str_count_sql nvarchar(500)
set @str_count_sql= 'select @totalcount=count('+@primarykey+') from ' + @tablenames + ' ' + @filter
exec sp_executesql @str_count_sql,n'@totalcount int=0 output',@recordcount output
endgo
GridView自定義分頁
在 asp.net 2.0 種提供了 gridview 控制項。該控制項的分頁比較方便,可以通過在 visual studio net 2005 種簡單設定即可實現各種分頁功能。1.預設分頁方式 1 是否允許分頁 gridview 的allowpaging 屬性。2 每頁記錄數 gridview 的...
GridView自定義分頁
該控制項的分頁比較方便,可以通過在visual studio net 2005種簡單設定即可實現各種分頁功能。1.預設分頁方式 1 是否允許分頁 gridview的allowpaging屬性。2 每頁記錄數 gridview的 pagesize 3 分頁導航條形式 gridview的 pagerse...
GridView自定義分頁
該控制項的分頁比較方便,可以通過在visual studio net 2005種簡單設定即可實現各種分頁功能。1.預設分頁方式 1 是否允許分頁 gridview的allowpaging屬性。2 每頁記錄數 gridview的 pagesize 3 分頁導航條形式 gridview的 pagerse...