NET DataList 分頁源碼 (轉)

 private const int PAGECOUNT = 9;
    private static int nPhotoCount = 0;
    private int nPhotoAllPageCount = 0;
    private int nPhotoCurrentPage = 0;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (ViewState["PhotoCurrentPage"] != null)
        {
            nPhotoCurrentPage = Int32.Parse(ViewState["PhotoCurrentPage"].ToString());
        }
        if (!Page.IsPostBack)
        {
            BindPhotoData();
            //重新綁定當前的頁碼   
            Label3.Text = (nPhotoCurrentPage + 1).ToString() + " ";
        }
    }
    private void BindPhotoData()
    {
        SqlDataAdapter ad = new SqlDataAdapter("select * from sysobjects","server=.;database=master;uid=sa;pwd=;");
        DataSet set = new DataSet();
        ad.Fill(set);
        nPhotoCount = set.Tables[0].Rows.Count;
        nPhotoAllPageCount = nPhotoCount / PAGECOUNT;
        if (nPhotoAllPageCount * PAGECOUNT < nPhotoCount)
        {
            nPhotoAllPageCount++;
        }
        //照片的總數量和總頁數
        Label1.Text = nPhotoAllPageCount.ToString() + " ";
        Label2.Text = nPhotoCount.ToString() + " ";
        DataSet ds =GetPhotoes(nPhotoCurrentPage * PAGECOUNT, PAGECOUNT);
        DataList1.DataSource = ds;
        DataList1.DataBind();
    }
    public DataSet GetPhotoes(int a,int b)
    {
        SqlDataAdapter ad = new SqlDataAdapter("select * from sysobjects", "server=.;database=master;uid=sa;pwd=;");
        DataSet set = new DataSet();
        ad.Fill(set, a, b, "sysobjects");
        return set;

    }
     private void Page_Click(object sender, System.EventArgs e)
    {
        String commangArg = ((LinkButton)sender).CommandArgument;
        switch (commangArg)
        {
            case "First":
                {
                    nPhotoCurrentPage = 0;
                    break;
                }
            case "Prev":
                {
                    nPhotoCurrentPage = (int)Math.Max(0, nPhotoCurrentPage - 1);
                    break;
                }
            case "Next":
                {
                    nPhotoCurrentPage = (int)Math.Min(nPhotoCount - 1, nPhotoCurrentPage + 1);
                    break;
                }
            case "Last":
                {
                    nPhotoCurrentPage = nPhotoCount - 1;
                    break;
                }
            default:
                {
                    break;
                }
        }
        ViewState["PhotoCurrentPage"] = nPhotoCurrentPage.ToString();
        BindPhotoData(); 
        Label3.Text = (nPhotoCurrentPage + 1).ToString() + " ";
    }

 
發佈了37 篇原創文章 · 獲贊 4 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章