用AspNetPager和ViewState分別實現asp.net分頁

來源:互聯網
上載者:User

以下介紹兩種分頁,用AspNetPager和ViewState

 

一.AspNetPager的用法

1. 複製AspNetPager.dll到bin目錄,在工具箱->選擇項->瀏覽,添加bin下的引用。

2. 從工具箱拖個AspNetPager,改如下屬性:

    PageSize--每頁顯示的記錄數

    CustomInfoHTML--自訂顯示文本,一般為“第%CurrentPageIndex%頁 共%PageCount%頁”

    ShowCustomInfoSection--顯示當前頁和總頁數資訊,值為Never,Right,Left

    AlwaysShow--總是顯示分頁控制項

    PageIndexBoxType--指定頁索引框的顯示類型,值為文字框,下拉式清單方塊

    ShowPageIndexBox--指定頁索引框的顯示方式,值為Always,Never,Auto

    TextAfterPageIndexBox--頁索引框後的常值內容,一般為“頁”

    TextBeforePageIndexBox--頁索引框前的常值內容,一般為“轉到”

 

顯示如下

 

 

3. 在AspNetPager的PageChanged事件中寫如下代碼:

     protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Databind();
    }

 

    private void Databind()
    {
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = List<T>;
        pds.AllowPaging = true;
        pds.PageSize = AspNetPager1.PageSize;        //AspNetPager1是ID
        AspNetPager1.RecordCount = pds.DataSourceCount;
        pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
        GridView1.DataSource = pds;        //綁定到GridView
        GridView1.DataBind();
    }

 

 

二. ViewState

  1.四個按鈕觸發以下同一個Click事件:

    protected void Button1_Click(object sender, EventArgs e)
    {

        PagedDataSource pds = new PagedDataSource();

        pds .DataSource = ds.Tables[0].DefaultView;
        pds .AllowPaging = true;
        pds .PageSize = 10;

        Button lb = (Button)sender;
        int page = 0;
        switch (lb.Text)
        {
            case "首頁":
                ViewState["vs"] = 0;
                break;
            case "下一頁":
                if (ViewState["vs"] != null)
                {
                    page = Convert.ToInt16(ViewState["vs"]);
                }
                if (page < pds.PageCount - 1)

                    page++;
                ViewState["vs"] = page;
                break;
            case "上一頁":
                if (ViewState["vs"] != null)
                {
                    page = Convert.ToInt16(ViewState["vs"]);
                }
                if (page > 0)

                    page--;
                ViewState["vs"] = page;
                break;
            case "尾頁":
                ViewState["vs"] = pds.PageCount - 1;
                break;
        }

        databind();

    }

 

    private void databind()
    {

        int page = 0;
        if (ViewState["vs"] != null)
        {
            page = Convert.ToInt16(ViewState["vs"]);
        }
        Status = "<font color=green>當前第" + (page + 1) + "/" + pds.PageCount + "頁</font>";
        pds.CurrentPageIndex = page;
        Repeater1.DataSource = pds;
        Repeater1.DataBind();

 

    }

 

相關文章

聯繫我們

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