asp.net分頁控制項AspNetPager的使用,使用傳統分頁和預存程序分頁

來源:互聯網
上載者:User

記錄一下,分頁控制項的使用,先記錄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控制項產生的程式碼,根據嚮導填入相關資訊,就可以產生預存程序的代碼

 

 

點擊將開啟:

根據具體細節填寫,點擊產生預存程序並複製到剪貼簿,可以複製出指令碼然後到資料庫管理系統中執行產生預存程序。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.