2012-03-23 75 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