大师们,小弟有个问题
我页面里面有个datalist控件,和一个objectdatasourse,我自己写了个类,有个方法
public PagedDataSource getware()
{
string strcon = ConfigurationManager.ConnectionStrings["SuperMarketDBConnectionString"].ConnectionString;
string str = "select * from T_Ware";
SqlConnection con=new SqlConnection ();
con.ConnectionString =strcon ;
SqlDataAdapter sda = new SqlDataAdapter(str, con);
DataSet ds = new DataSet();
sda.Fill(ds, "Ware");
PagedDataSource pds= new PagedDataSource();
pds.DataSource = ds.Tables["Ware"].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 6;
return pds;
}
这个方法给了objectdatasourse,我数据库里面有14条数据,这么说有3页,我datalist里面也编辑模版了,绑定了某些列,我界面还有2个按钮,怎么实现上一页,和下一页啊
------解决方案--------------------
int currentPage = Convert.ToInt32(LabelNowPage.Text); //这是用一个label显示你当前的页数。
ps.AllowPaging = true;
中间省略了数据库的操作
ps.PageSize = 5;
ps.CurrentPageIndex = currentPage - 1;
LinkButtonTop.Enabled = true;
LinkButtonPrev.Enabled = true;
LinkButtonNext.Enabled = true;
LinkButtonLast.Enabled = true;
if (currentPage == 1)
{
LinkButtonTop.Enabled = false;
LinkButtonPrev.Enabled = false;
}
if (currentPage == ps.PageCount)
{
LinkButtonNext.Enabled = false;
LinkButtonLast.Enabled = false;
}
LabelCount.Text = Convert.ToString(ps.PageCount);//这是显用总页数的label
this.DataList1.DataSource = ps;
this.DataList1.DataBind();
}
上面的代码是数据绑定函数里面.
protected void LinkButtonPrev_Click(object sender, EventArgs e)
{
LabelNowPage.Text = Convert.ToString(Convert.ToInt32(LabelNowPage.Text) - 1);
this.dlbind();
}//这个就是实现上一页的函数
我这是有首页,上一页,下一页,尾页四个
不知道能看得懂不?