比如说我用datalist控件来显示数据库,因为内容过多涉及到分页听说AspNetPager不错,很方便,但就是不知道如何用请问在后台代码里要做如何设置: protected void Page_Load(object sender, EventArgs e)
{ string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath ("db1.mdb");
OleDbConnection ConnAcc=new OleDbConnection (strConn);
ConnAcc.Open ();
string strSQL="SELECT * FROM wzw_client";
OleDbDataAdapter da=new OleDbDataAdapter(strSQL,ConnAcc);
DataSet ds=new DataSet();
da.Fill(ds,"wzw");
ConnAcc.Close (); wzwList.DataSource = ds; //设置数据源
wzwList.DataBind(); //绑定数据
}
官方网上有说明!
不过建议用存储过程比较好!
去这里看看..
www.webdiyer.com
protected void GV_Bind()//绑定控件
{
Guid id = new Guid(departid);
DataCollection<DepartInfo3SDO> depart3Collection = getDepart3Service().FindByParentID(id);
AspNetPager1.RecordCount = depart3Collection.Count;
PagedDataSource pds = new PagedDataSource();
pds.DataSource = depart3Collection;
pds.AllowPaging = true;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.PageSize = AspNetPager1.PageSize;
GV_DepartInfo3.DataSource = pds;
GV_DepartInfo3.DataBind();
} protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
GV_Bind();
}
{
if(!IsPostBack){
string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath ("db1.mdb");
OleDbConnection ConnAcc=new OleDbConnection (strConn);
OleDbCommand cmd=new OleDbCommand("SELECT count(*) FROM wzw_client",ConnAcc);
ConnAcc.Open();
AspNetPager1.RecordCount=(int)cmd.ExecuteScalar(); //设置总记录数
ConnAcc.Close();
bindData(); 绑定当前页数据
}
} void bindData(){
string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath ("db1.mdb");
OleDbConnection ConnAcc=new OleDbConnection (strConn);
string strSQL="SELECT * FROM wzw_client";
OleDbDataAdapter da=new OleDbDataAdapter(strSQL,ConnAcc);
DataSet ds=new DataSet();
da.Fill(ds,AspNetPager1.StartRecordIndex-1,AspNetPager1.PageSize,"wzw"); //只填充当前页的数据
wzwList.DataSource = ds; //设置数据源
wzwList.DataBind(); //绑定数据
} 然后编写PageChanged事件处理程序,在该处理程序中重新调用 bindData()方法就行了。
create procedure XXXProc
(
@startRowIndex int,
@maximumRows int
)
as
select columnList from (select columnList, ROW_NUMBER() OVER (ORDER BY orderColumn) as RowRank) AS TableWithRowNumbers
where RowRank > @startRowIndex and RowRank <= (@startRowIndex + @maximumRows)数据访问层通过这个存储过程提供数据给业务逻辑层,另外再向业务逻辑层提供一个返回数据总数的接口(select count(*) from table)。业务逻辑层建一个能接受startRowIndex, maximumRows为参数的方法来调用数据访问层对应的方法,该方法又调用上面这个存储过程。
再建一个调用数据访问层返回的数据总数的方法。配置ObjectDataSource的SelectCountMethod,再把向导生成的ObjectDataSource的<SelectParameters>段去掉(因为GridView会自动向ObjectDataSource传递startRowIndex, maximumRows值)。大概也就这样了。
void bindData(){
string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath ("db1.mdb");
OleDbConnection ConnAcc=new OleDbConnection (strConn);
string strSQL="SELECT * FROM wzw_client";
OleDbDataAdapter da=new OleDbDataAdapter(strSQL,ConnAcc);
DataSet ds=new DataSet();
da.Fill(ds,AspNetPager1.StartRecordIndex-1,AspNetPager1.PageSize,"wzw"); //只填充当前页的数据
wzwList.DataSource = ds; //设置数据源
wzwList.DataBind(); //绑定数据
}