这次使用PagedDataSource
类来实现DataList
分页技术,因为这个类封装了DataGird
的分页属性,
我们来使用它来实现分页,我在这里做到了,第一页,上一页,下一页,最后一页,跳转到第几页这几个功能。这些功能应该都比较实用。希望对大家有所用,给大家看代码吧。
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>
public class 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;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
this.bindtoGL();
}
}
//绑定数据到DataList
private void 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();
}
//这个是最后一页的按钮点击事件
private void 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();
}
}
}