記錄一下,分頁控制項的使用,先記錄AspNetPager分頁控制項的使用
AspNetPager分頁的控制項的是:http://www.webdiyer.com/Controls/AspNetPager/Downloads
線上協助文檔:http://www.webdiyer.com/AspNetPagerDocs/index.html
AspNetPager比較重要的幾個屬性
CurrentPageIndex 擷取或設定當前顯示頁的索引。
PageSize 擷取或設定每頁顯示的項數。
PageCount 擷取所有要分頁的記錄需要的總頁數。
CustomInfoHTML 擷取或設定在顯示在使用者自訂資訊區的使用者自訂HTML常值內容。
FirstPageText 擷取或設定為第一頁按鈕顯示的文本。
LastPageText 擷取或設定為最後一頁按鈕顯示的文本。
PrevPageText 擷取或設定為上一頁按鈕顯示的文本。
RecordCount 擷取或設定需要分頁的所有記錄的總數。
AlwaysShow 擷取或設定一個值,該值指定是否總是顯示AspNetPager分頁按件,即使要分頁的資料只有一頁。
ShowPageIndex 擷取或設定一個值,該值指示是否在頁導航元素中顯示頁索引數值按鈕。
ShowPrevNext 擷取或設定一個值,該值指示是否在頁導航元素中顯示上一頁和下一頁按鈕。
UrlPaging 擷取或設定是否啟用url來傳遞分頁資訊。
1.aspnetpager使用傳統方式分頁
使用Repeater控制項和AspNetPager控制項實現傳統分頁,
private void BindRepeater()
{
string sql = "select * from tb_Roles";//自訂的SQL語句
int recordcount;
SqlCommand cmd = new SqlCommand(sql, GetConnection());
cmd.CommandType = CommandType.Text;
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
int startRow = (this.AspNetPager1.CurrentPageIndex - 1) * this.AspNetPager1.PageSize;
ada.Fill(ds, startRow, this.AspNetPager1.PageSize, "table");
recordcount = GetPageRecord(sql);
this.AspNetPager1.RecordCount = recordcount;
this.Repeater1.DataSource = ds;
this.Repeater1.DataBind();
}
另外在AspNetPager分頁控制項PageChanged事件中綁定。
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
this.BindRepeater();
}
另外擷取總記錄數的方法
public int GetPageRecord(string sql)
{
sql = System.Text.RegularExpressions.Regex.Replace(sql, "ORDER BY.*", "");
sql = "select count(*) from (" + sql + ") as temp";
SqlCommand cmd = new SqlCommand(sql, GetConnection());
cmd.Connection.Open();
int recordcount = (int)cmd.ExecuteScalar();
cmd.Connection.Close();
return recordcount;
}
2.aspnetpager中使用預存程序分頁
調用預存程序擷取資料
private void BindListView()
{
this.AspNetPager2.RecordCount = GetPageRecord("select * from tb_Groups");//擷取總記錄數
SqlCommand cmd = new SqlCommand("pager", GetConnection());
cmd.CommandType = CommandType.StoredProcedure;//使用預存程序
int startRow = (this.AspNetPager2.CurrentPageIndex - 1) * this.AspNetPager2.PageSize;//起始點
int endRow = this.AspNetPager2.CurrentPageIndex * this.AspNetPager2.PageSize;
SqlParameter[] parameters = new SqlParameter[]{
new SqlParameter("@startIndex",startRow),
new SqlParameter("@endIndex",endRow),
new SqlParameter("@docount",'0'),
};
cmd.Parameters.AddRange(parameters);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ada.Fill(ds,"table");
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
同樣需要在PageChanged事件調用Binder 方法
最後預存程序的代碼是用AspNetPager控制項產生的程式碼,根據嚮導填入相關資訊,就可以產生預存程序的代碼
點擊將開啟:
根據具體細節填寫,點擊產生預存程序並複製到剪貼簿,可以複製出指令碼然後到資料庫管理系統中執行產生預存程序。