2012-03-23 81 views

回答

6

使用PagedDataSource類封裝了數據綁定控件允許它進行分頁的分頁相關的屬性.......

//creating the PagedDataSource instance.... 
pg = new PagedDataSource(); 
pg.DataSource = myTable; 
pg.AllowPaging = true; 
pg.PageSize = 10; 

//Binding pg to datalist 
dl.DataSource = pg;//dl is datalist 
dl.DataBind(); 
+0

我嘗試它,但它給錯誤PagedDataSource pg = new PagedData資源(); pg.DataSource = objclsfileupload.selectPendingContent(Session [「UserId」]。ToString()); pg.AllowPaging = true; pg.PageSize = 1; DataList1.DataSource = pg; DataList1.DataBind();錯誤:無法計算未實現ICollection的數據源的計數。 – 2012-03-23 10:02:49

+0

你能指定錯誤嗎? – 2012-03-23 10:03:19

+0

錯誤:無法計算沒有實現ICollection的數據源的計數 – 2012-03-23 10:09:24

1

請參閱本Adding Paging Support to the Repeater or DataList with the PagedDataSource Class

創建頁面級頁面數據源對象。

PagedDataSource objPds; 

// Populate the repeater control with the DataSet at page init or pageload 
objPds = new PagedDataSource(); 
objPds.DataSource = ds.Tables[0].DefaultView; 

// Indicate that the data should be paged 
objPds.AllowPaging = true; 

// Set the number of items you wish to display per page 
objPds.PageSize = 3; 

沿此保存視圖狀態或會話中的當前頁面索引。

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 
    ItemsGet(); 
} 

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

    // Reload control 
    ItemsGet(); 
} 

檢查這其中也: Efficient Data Paging with the ASP.NET 2.0 DataList Control and ObjectDataSource

2

我得到的答案..

DataTable dt = new DataTable(); 
      var data = objclsfileupload.selectPendingContent(Session["UserId"].ToString());// Iqueryable data 
      var data2 = data.GetEnumerator(); 
      dt.Columns.Add("agegroup"); 
      dt.Columns.Add("contenttype"); 


      while (data2.MoveNext()) 
      { 
       var record = (filuploadclass)data2.Current; 
       dt.Rows.Add(record.agegroup, record.ContenetType); 

      } 

      pg.DataSource =dt.DefaultView ; 

      pg.AllowPaging = true; 
      pg.PageSize = 1; 
      DataList1.DataSource = pg; 
      DataList1.DataBind(); 
+1

很好的答案..你真的配得下這個..改變你的查詢錯誤'錯誤:無法計算沒有實現ICollection的數據源計數.'你有答案。它不涉及設定的尋呼。使用'data.ToList()'而不是手動獲取單個項目。 – 2012-03-26 07:21:58