当前位置:编程学习 > C#/ASP.NET >>

求ASP.NET的DataList分页程序

用DataList分页
要求:数据库连接为server=.;database=WP4;uid=sa;pwd=sa。
连接的表为grxxb。分页为每页三个记录。
表的内容为:上图。
追问:能不能给全一点啊
把前台和后台的都给我,
谢谢啊,本人刚学。
答案:
//以下是我以前毕业设计里用的分页,可以参考一下,我也是在书上模仿的
public void Bind()
{
string myid =(string)Session["USERID"];
int flag=Convert.ToInt32(Request.QueryString["flag"].ToString());
//获取当前页数
int curpage = Convert.ToInt32(this.labPage.Text);
//生成PagedDataSource的实例
PagedDataSource ps = new PagedDataSource();
MyClass myclass = new MyClass();//创建一个MyClass类的方法
string strCon = myclass.strCon; //初始化连接对象
SqlConnection con = new SqlConnection(strCon);
con.Open();
string sql;
sql = "select * from QUESTION where FID like '" + myid + "'order by FDATE DESC";
if (flag == 2)
sql = "select * from QUESTION where ID in(select ID from HUIFU where HUIFU.HID='" + myid + "')";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "QUESTION");
//获取数据源
ps.DataSource = ds.Tables["QUESTION"].DefaultView;
//启用分页
ps.AllowPaging = true;
//每页显示5条数据
ps.PageSize = 5;
//设置当前页的索引
ps.CurrentPageIndex = curpage - 1;
this.lnkbtnFrist.Enabled = true;
this.lnkbtnNext.Enabled = true;
this.lnkbtnLast.Enabled = true;
this.lnkbtnFront.Enabled = true;
if (curpage == 1) //等于第一页
{
this.lnkbtnFrist.Enabled = false;//不显示第一页按钮
this.lnkbtnFront.Enabled = false;//不显示上一页按钮
}
if (curpage == ps.PageCount) //等于最后一页
{
this.lnkbtnNext.Enabled = false;//不显示下一页
this.lnkbtnLast.Enabled = false;//不显示最后一页
}
//显示总页数
this.labBackPage.Text = Convert.ToString(ps.PageCount);
// this.DataList1.DataSource = ps;
//设置数据源的关键字段
this.DataList1.DataKeyField = "ID";
DataList1.DataSource = ps;
DataList1.DataBind();
con.Close();
}
public void Page_Load(Object src,EventArgs e) 
{
SqlConnetion myConnetion=new SqlConnetion();
myConnetion.SqlConnetionString="user id=sa;password=sa;initial catalog=WP4;data source=.";
SqlDataAdapter sqlCommand=new sqlDataAdapter("select * from grxxb",myConnetion);
DataSet ds=new DataSet();
sqlCommand.Fill(ds);

//对PagedDataSource 对象的相关属性赋值
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 3;
int CurPage;

//当前页面从Page查询参数获取
if (Request.QueryString["Page"] != null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage=1;

objPds.CurrentPageIndex = CurPage-1;
lblCurrentPage.Text = "Page: " + CurPage.ToString();

if (!objPds.IsFirstPage)
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1);

if (!objPds.IsLastPage)
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(CurPage+1);

//把PagedDataSource 对象赋给Repeater控件
Repeater1.DataSource=objPds;
Repeater1.DataBind();
}

上一个:Active Server Pages, ASP 0113 (0x80004005)
下一个:asp,asp.net,php,jsp有什么优缺点?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,