如何在asp.net中的DataList控件中设置分页?

时间:2012-03-23 09:35:54

标签: asp.net

实际上Datalist不包含allowpaging属性。那么如何在datalist中设置分页。

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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();

答案 2 :(得分: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

相关问题