執行個體:asp.net中GridView實現分頁功能

來源:互聯網
上載者:User

要實現GrdView分頁的功能

操作如下:

1、更改GrdView控制項的AllowPaging屬性為true。
2、更改GrdView控制項的PageSize屬性為 任意數值(預設為10)
3、更改GrdView控制項的PageSetting->Mode為Numeric等(預設為Numeric)該屬性為分頁樣式。
GridView屬性設定好了,從頁面上也能看到分頁樣式。

現在開始實現分頁的功能:

1、在<<asp:GridView ID=......>後添加,OnPageIndexChanging="GridView1_PageIndexChanging"
2、在對應的aspx.cs中添加:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
InitPage(); //重新綁定GridView資料的函數
}

例子:

功能:GridView分頁使用圖片按鈕並添加分頁碼顯示。

預設情況下GridView的分頁按鈕如果以圖片來顯示就無法顯示文字,這樣就無法知道當前所在的頁數。於是,添加分頁代碼顯示就可以顯示所在分頁的索引數字了。

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GridView_Page : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //設定分頁的圖片按鈕,這些都可以在控制項的屬性工作表上的pagersetting裡設定
        if (!IsPostBack)
        {
            GridView1.Caption = "這是一個GridView的小實驗"; 
                  //Caption屬性類似於表名,顯示在控制項的正上方。
            GridView1.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast;
            GridView1.PagerSettings.NextPageImageUrl = "img/next.gif";
            GridView1.PagerSettings.PreviousPageImageUrl = "img/pre.gif";
            GridView1.PagerSettings.FirstPageImageUrl = "img/first.gif";
            GridView1.PagerSettings.LastPageImageUrl = "img/last.gif";
            GridView1.PageSize = 10;  //每頁最多顯示10條記錄;
            BindData();
        }
    }
    private void BindData()
    {
        //將資料部署到GridView中
        string Constr = "server=localhost; uid=sa;pwd=123456;database=NorthWind";
        string sqlstr = "select * from products";
        SqlConnection con = new SqlConnection(Constr);
        SqlDataAdapter ad = new SqlDataAdapter(sqlstr, con);
        DataSet ds = new DataSet();
        ad.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        //進行分頁之後,重新部署資料
        BindData();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //分頁完成之前
        GridView1.PageIndex = e.NewPageIndex;
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        //添加分頁碼顯示
        GridViewRow bottomPagerRow = GridView1.BottomPagerRow;
        Label bottomLabel = new Label();
        bottomLabel.Text = "目前所在分頁:(" + (GridView1.PageIndex + 1) + "/" + GridView1.PageCount + ")";
        bottomPagerRow.Cells[0].Controls.Add(bottomLabel);
    }
}

聯繫我們

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