首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

分页的有关问题

2012-01-15 
分页的问题定义了一个:PagedDataSourcepdsnewPagedDataSource()      pds.PageSize5我使用的是DataLis

分页的问题
定义了一个: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)

this.HlkPrevious.NavigateUrl=Request.CurrentExecutionFilePath+ "?pageIndex= "+Convert.ToString(CurrentPage-1);



//下一页导航

if(!pds.IsLastPage)

this.HlkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?pageIndex= "+Convert.ToString(CurrentPage+1);

//第一页导航

this.HlkFirst.NavigateUrl=Request.CurrentExecutionFilePath+ "?pageIndex= "+1;

//最后一页导航 this.HlkLast.NavigateUrl=Request.CurrentExecutionFilePath+ "?pageIndex= "+pds.PageCount;

this.DataList1.DataSource=pds;

this.DataList1.DataBind();

}

//这个是最后一页的按钮点击事件

privatevoid LinkButton1_Click(object sender, System.EventArgs e)

{

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;

if(this.TbPageIndex.Text.ToString()== " "||Convert.ToInt32(this.TbPageIndex.Text.Trim())> =pds.PageCount)

{

Response.Write( "输入不能为空,或则数字不能超过总页数! ");

}

else

{

pds.CurrentPageIndex=Convert.ToInt32(this.TbPageIndex.Text.Trim())-1;

}

this.LblCurrent.Text=this.TbPageIndex.Text;

this.DataList1.DataSource=pds;

this.DataList1.DataBind();

}

}

}

热点排行