日期:2014-05-18  浏览次数:20387 次

分页的问题
定义了一个:PagedDataSource   pds   =   new   PagedDataSource();
      pds.PageSize   =   5;
我使用的是DataList控件,绑定的数据库。
用什么方法把pds   与DataList关联起来!
如何给我绑定的数据分页吗?

谢谢~

------解决方案--------------------
using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

namespace DataListTest1

{

/// <summary>

/// WebForm1的摘要说明。

/// </summary>

publicclass WebForm1 : System.Web.UI.Page

{

protected System.Web.UI.WebControls.DataList DataList1;

protected System.Web.UI.WebControls.HyperLink HlkFirst;

protected System.Web.UI.WebControls.HyperLink HlkPrevious;

protected System.Web.UI.WebControls.HyperLink HlkNext;

protected System.Web.UI.WebControls.HyperLink HlkLast;

protected System.Web.UI.WebControls.Label LblTotalRecord;

protected System.Web.UI.WebControls.Label LblTotalPages;

protected System.Web.UI.WebControls.Label LblCurrent;

protected System.Web.UI.WebControls.LinkButton LinkButton1;

protected System.Web.UI.WebControls.TextBox TbPageIndex;

privatevoid Page_Load(object sender, System.EventArgs e)

{

//在此处放置用户代码以初始化页面

if(!this.IsPostBack)

{

this.bindtoGL();

}

}

//绑定数据到DataList

privatevoid bindtoGL()

{

SqlConnection con=new SqlConnection( "server=.;database=Northwind;uid=sa;pwd= ");

con.Open();

SqlDataAdapter sda=new SqlDataAdapter();

sda.SelectCommand=new SqlCommand( "select * from Orders ",con);

DataSet ds=new DataSet();

sda.Fill(ds, "orders ");

PagedDataSource pds=new PagedDataSource();

pds.DataSource=ds.Tables[ "orders "].DefaultView;

pds.AllowPaging=true;

pds.PageSize=12;

int CurrentPage;

if(Request.QueryString[ "pageIndex "]!=null)

{

CurrentPage=Convert.ToInt32(Request.QueryString[ "pageIndex "]);

}

else

{

CurrentPage=1;

}



pds.CurrentPageIndex=CurrentPage-1;

//CurrentPageIndex是从0页开始的所以要减1

this.LblCurrent.Text=CurrentPage.ToString();//显示是从第1页开始

//上一页导航

if(!pds.IsFirstPage)