ASP.NET中分頁顯示

來源:互聯網
上載者:User

標籤:style   blog   class   code   java   tar   

分頁詳解,ASP.NET書本  “資料空間應用”

(1)使用GridView控制項分頁顯示 步奏:1.添加GridView控制項 2.將GrideView控制項的AllowPaging屬性設定為True,表示允許分頁  3.將PageSize屬性設定一個數字,用來控制每頁中顯示的記錄數 4.最後在GridView控制項的PageIndexChanging事件中設定GridView控制項的PageIndex屬性為當前頁的索引值,並重新綁定GridView控制項

(2)DataList控制項中的資料

using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            Bind();//資料繫結        }    }    //進行資料繫結的方法    public void Bind()    {        int CurrentPage = Convert.ToInt32(labNowPage.Text);  ///labNowPage標籤  記錄當前頁碼        PagedDataSource ps = new PagedDataSource();//產生PagedDataSource的執行個體        //執行個體化SqlConnection對象        SqlConnection sqlCon = new SqlConnection();        //執行個體化SqlConnection對象串連資料庫的字串        sqlCon.ConnectionString = "server=.;uid=sa;pwd=;database=db_09";        //定義SQL語句        string SqlStr = "select * from tb_Student";        //執行個體化SqlDataAdapter對象        SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);        //執行個體化資料集DataSet        DataSet ds = new DataSet();        da.Fill(ds, "tb_Student");        ps.DataSource = ds.Tables["tb_Student"].DefaultView;        ps.AllowPaging = true; //是否可以分頁        ps.PageSize = 2; //顯示的數量        ps.CurrentPageIndex = CurrentPage - 1; //取得當前頁的頁碼        lnkbtnFront.Enabled = true;        lnkbtnFirst.Enabled = true;        lnkbtnNext.Enabled = true;        lnkbtnLast.Enabled = true;        if (CurrentPage == 1)        {            lnkbtnFirst.Enabled = false;//不顯示第一頁按鈕            lnkbtnFront.Enabled = false;//不顯示上一頁按鈕        }        if (CurrentPage == ps.PageCount)        {            lnkbtnNext.Enabled = false;//不顯示下一頁            lnkbtnLast.Enabled = false;//不顯示最後一頁        }        this.labCount.Text = Convert.ToString(ps.PageCount);//記錄總頁數        this.DataList1.DataSource = ps;        this.DataList1.DataKeyField = "ID";        this.DataList1.DataBind();    }    //首頁    protected void lnkbtnFirst_Click(object sender, EventArgs e)    {        this.labNowPage.Text = "1";        this.Bind();    }    //上一頁    protected void lnkbtnFront_Click(object sender, EventArgs e)    {        this.labNowPage.Text = Convert.ToString(Convert.ToInt32(this.labNowPage.Text) - 1);        this.Bind();    }    //下一頁    protected void lnkbtnNext_Click(object sender, EventArgs e)    {        this.labNowPage.Text = Convert.ToString(Convert.ToInt32(this.labNowPage.Text) + 1);        this.Bind();    }    //尾頁    protected void lnkbtnLast_Click(object sender, EventArgs e)    {        this.labNowPage.Text = this.labCount.Text;        this.Bind();    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.