2013-05-17 56 views
0

我正在使用一個asp.net頁面。我正在使用一個DataList控件。我看到這個控件沒有分頁模板,所以我使用了PagedDataSource控件(第一次)。我需要顯示上一頁,下一頁和編號的頁面,如:使用PagedDatasource在DataList中分頁

previous 1 2 3 4 5 next View ALL 

這是我迄今所做的:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 

      PagedDataSource pg = new PagedDataSource(); 
      pg.DataSource = HRCompany.GetList().DefaultView; 

      pg.AllowPaging = true; 
      pg.PageSize = 8; 
      CompaniesDataList.DataSource = pg; 
      CompaniesDataList.DataBind(); 
     } 
    } 

    public int CurrentPage 
    { 
     get 
     { 
      // look for current page in ViewState 
      object o = this.ViewState["_CurrentPage"]; 
      if (o == null) 
       return 0; // default page index of 0 
      else 
       return (int)o; 
     } 

     set 
     { 
      this.ViewState["_CurrentPage"] = value; 
     } 
    } 


    private void cmdPrev_Click(object sender, System.EventArgs e) 
    { 
     // Set viewstate variable to the previous page 
     CurrentPage -= 1; 

     // Reload control 
     PagedDataSource pg = new PagedDataSource(); 
     pg.DataSource = HRCompany.GetList().DefaultView; 
     CompaniesDataList.DataSource = pg; 
     CompaniesDataList.DataBind(); 
    } 

    private void cmdNext_Click(object sender, System.EventArgs e) 
    { 
     // Set viewstate variable to the next page 
     CurrentPage += 1; 

     // Reload control 
     PagedDataSource pg = new PagedDataSource(); 
     pg.DataSource = HRCompany.GetList().DefaultView; 
     CompaniesDataList.DataSource = pg; 
     CompaniesDataList.DataBind(); 
    } 

,但我沒有看到一個或下一個按鈕翻頁點擊。另外,如何根據記錄顯示頁碼,並在用戶單擊查看全部時顯示所有記錄。另外,我是否需要更改存儲過程以允許分頁?

我的存儲過程是這樣的:

ALTER PROCEDURE [dbo].[hr_Companies_GetByIDAndName] 

    @CompanyID INT, 
    @CompanyName VARCHAR(100), 
    @Lang int = 1 

AS 

BEGIN 

    SET NOCOUNT ON; 



    SELECT C.* 

    FROM dbo.hr_Companies C 

    WHERE C.Deleted = 0 
    AND C.CompanyID = @CompanyID 
    AND @CompanyName = CASE WHEN @Lang = 1 then NameLang1 ELSE NameLang2 END 
END 

請建議。

回答

1

兄弟,

您的野趣的問題迫使我編譯和解決你的代碼的問題,而我在工作:) 的解決方案是如下:

 PagedDataSource pg = null; 
     protected void Page_Init(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       pg = new PagedDataSource(); 

      } 
     } 

     protected void Page_Load(object sender, EventArgs e) 
      { 
     if (!IsPostBack) 
       { 


        pg.DataSource = dt.DefaultView; 
        pg.AllowPaging = true; 
        pg.PageSize = 2; 



        dlPagedControl.DataSource = pg; 

        Session["DataTable"] = dt; //dt is just a datatable replace it with whatever you need 
        Session["CurrentPage"] = pg.CurrentPageIndex; 

        dlPagedControl.DataBind(); 

       } 
    } 


     protected void cmdPrev_Click(object sender, EventArgs e) 
      { 

       if(int.Parse(Session["CurrentPage"].ToString())!=0) 
       { 
       if (pg == null) 
       { 
        pg = new PagedDataSource(); 
        pg.PageSize = 2; 
        pg.AllowPaging = true; 
       } 
       pg.DataSource = (Session["DataTable"] as DataTable).DefaultView; 


       pg.CurrentPageIndex = (int.Parse(Session["CurrentPage"].ToString())) - 1; 

       dlPagedControl.DataSource = pg; 
       dlPagedControl.DataBind(); 
       Session["CurrentPage"] = pg.CurrentPageIndex; 

        lable1.Text=pg.CurrentPageIndex; 
       } 





      } 
    protected void cmdNext_Click_Click(object sender, EventArgs e) 
      { 


       if (pg == null) 
       { 
        pg = new PagedDataSource(); 
        pg.PageSize = 2; 
        pg.AllowPaging = true; 
       } 
       pg.DataSource = (Session["DataTable"] as DataTable).DefaultView; 
       pg.CurrentPageIndex = (int.Parse(Session["CurrentPage"].ToString())) + 1; 

       dlPagedControl.DataSource = pg; 
       dlPagedControl.DataBind(); 
       Session["CurrentPage"] = pg.CurrentPageIndex; 
//show Current Page in Label 
lable1.Text=pg.CurrentPageIndex; 
      }