本人學慣用asp.net做網站時間並不長,在學習過程中,分頁成為一個很大的問題,asp.net自己的組件分布的視覺效果總覺的差的很多,而且使用起來並不是很順手(也許是我技術太差了),所以我選擇在網上找專門的分頁組件,這裡選用了AspNetPager,這個一位陝西的程式員開發並封裝成Dll檔案,這個分頁組件使用起來很方便,並整合了預存程序產生工具,對我這個新手來說很有協助的.儘管這個組件使用很簡單,不過如果新手開始用這個組件的話可能還是要花上一定時間(我花了半天),現我向大家詳細介紹這個組件的使用方法:
一、用到的組件參數
RecordCount 總記錄數
StartRecordIndex 開始記錄編號(用於預存程序中)
EndRecordIndex結束記錄編號(用於預存程序中)
二、使用方法
在Page_Load中使用。
例:
public partial class Default4 : System.Web.UI.Page
{
DataOperation mydo = new DataOperation();
if (!IsPostBack) //如果不是回傳就執行,用於初始化
{
DataOperation mydo1 = new DataOperation(); //執行個體化資料庫連接
string sqlstr1 = "exec fy 0,0,1"; //定義Sql語句,使用了fy儲存函數
//初始化RecordCount,mydo1.GetExeSqlCount返回一個int
AspNetPager1.RecordCount = mydo1.GetExeSqlCount(sqlstr1);
//定義Sql語句,使用了fy儲存函數
string sqlstr = "exec fy " + AspNetPager1.StartRecordIndex + "," + AspNetPager1.EndRecordIndex + ",0";
//給GridView1指定一資料來源, mydo.GetExeSqlDataTable返回一個DataTable
this.GridView1.DataSource = mydo.GetExeSqlDataTable(sqlstr);
this.GridView1.DataBind();
}
protected void AspNetPager1_ PageChanged (object sender, EventArgs e)
{// AspNetPager1組件的PageChanged事件
string sqlstr = "exec fy " + AspNetPager1.StartRecordIndex + "," + AspNetPager1.EndRecordIndex + ",0";
this.GridView1.DataSource = mydo.GetExeSqlDataTable(sqlstr);
this.GridView1.DataBind();
}
}
這個組件中還有很多其他的功能,大家在使用過程就會發現,設定是可視化的,這裡我就不多說了,下面是本文中用到的預存程序,相信對大家理解上面的代碼有一定協助,本文中也一起附上。
預存程序:
ALTER procedure fy
(@startIndex int,
@endIndex int,
@docount int)
as
set nocount on
if(@docount=1)
select count(*) from Xt_Article
else
begin
declare @indextable table(id int identity(1,1),nid int)
set rowcount @endIndex
insert into @indextable(nid) select id from Xt_Article order by id asc
select * from Xt_Article O,@indextable t where O.id=t.nid
and t.id between @startIndex and @endIndex order by t.id
end
set nocount off