
datalist分页按钮做起来了,数据库有多少数据都会显示出来,分页按钮不起作用
intPageSize,RecordCount,PageCount,CurrentPage;//每页条数,总条数,总页数,当前页数SqlConnectionMyConn;...
int PageSize, RecordCount, PageCount, CurrentPage;//每页条数,总条数,总页数,当前页数
SqlConnection MyConn;
public void Page_Load(Object src, EventArgs e)
{
PageSize = 8;//每页8条记录
string MyConnString =
@"Server=(local)\SQLEXPRESS;Integrated Security=SSPI;Database=image;Persist Security Info=True";
MyConn = new SqlConnection(MyConnString);
MyConn.Open();
if (!Page.IsPostBack)
{
CurrentPage = 0 ViewState["IndexOfPage"] = 0 RecordCount = CalculateRecord
lblRecordCount.Text = RecordCount.ToString();
PageCount = RecordCount / PageSize;//计算总共有多少页
lblPageCount.Text = PageCount.ToString();
ViewState["CountOfPage"] = PageCount;//显示总页数
ListBind }
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from images";
SqlCommand MyComm = new SqlCommand(strCount, MyConn);
SqlDataReader dr = MyComm.ExecuteReader();
if (dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else {intCount = 0}
dr.Close();
return intCount;
}
ICollection CreateSource()
{
int StartIndex;
StartIndex = CurrentPage * PageSize;
string strSel = "select * from images";
DataSet ds = new DataSet();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, MyConn);
MyAdapter.Fill(ds, StartIndex, PageSize, "images
return ds.Tables["images"].DefaultView;
}
//将数据绑定到Datalist控件
public void ListBind()
{
DataList1.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if (CurrentPage == PageCount - 1) lbnNextPage.Enabled = false;//当为最后一页时,下一页链接按钮不可用
if (CurrentPage == 0) lbnPrevPage.Enabled = false;//当为第一页时,上一页按钮不可用
lblCurrentPage.Text = (CurrentPage + 1).ToString();//当前页数
}
//编写的按钮点击事件
public void Page_OnClick(Object sender, CommandEventArgs e)
{
CurrentPage = (int)IndexOfPage;//获得当前页索引
PageCount = (int)CountOfPage;//获得总页数
string cmd = e.CommandName;//判断cmd,以判定翻页方向
switch (cmd)
{
case "next"://下一页
if (CurrentPage < (PageCount - 1)) CurrentPage++;
break;
case "prev"://上一页
if (CurrentPage > 0) CurrentPage--;
break;
}
IndexOfPage = CurrentPage;//获得当前页
ListBind();//重新将DataList绑定到数据库
}
上面的代码基本上能调用出数据库的数据,但是Datalist会把全部的数据都显示出来,分页按钮不起作用。没有财富值情高手帮忙 展开
SqlConnection MyConn;
public void Page_Load(Object src, EventArgs e)
{
PageSize = 8;//每页8条记录
string MyConnString =
@"Server=(local)\SQLEXPRESS;Integrated Security=SSPI;Database=image;Persist Security Info=True";
MyConn = new SqlConnection(MyConnString);
MyConn.Open();
if (!Page.IsPostBack)
{
CurrentPage = 0 ViewState["IndexOfPage"] = 0 RecordCount = CalculateRecord
lblRecordCount.Text = RecordCount.ToString();
PageCount = RecordCount / PageSize;//计算总共有多少页
lblPageCount.Text = PageCount.ToString();
ViewState["CountOfPage"] = PageCount;//显示总页数
ListBind }
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from images";
SqlCommand MyComm = new SqlCommand(strCount, MyConn);
SqlDataReader dr = MyComm.ExecuteReader();
if (dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else {intCount = 0}
dr.Close();
return intCount;
}
ICollection CreateSource()
{
int StartIndex;
StartIndex = CurrentPage * PageSize;
string strSel = "select * from images";
DataSet ds = new DataSet();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, MyConn);
MyAdapter.Fill(ds, StartIndex, PageSize, "images
return ds.Tables["images"].DefaultView;
}
//将数据绑定到Datalist控件
public void ListBind()
{
DataList1.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if (CurrentPage == PageCount - 1) lbnNextPage.Enabled = false;//当为最后一页时,下一页链接按钮不可用
if (CurrentPage == 0) lbnPrevPage.Enabled = false;//当为第一页时,上一页按钮不可用
lblCurrentPage.Text = (CurrentPage + 1).ToString();//当前页数
}
//编写的按钮点击事件
public void Page_OnClick(Object sender, CommandEventArgs e)
{
CurrentPage = (int)IndexOfPage;//获得当前页索引
PageCount = (int)CountOfPage;//获得总页数
string cmd = e.CommandName;//判断cmd,以判定翻页方向
switch (cmd)
{
case "next"://下一页
if (CurrentPage < (PageCount - 1)) CurrentPage++;
break;
case "prev"://上一页
if (CurrentPage > 0) CurrentPage--;
break;
}
IndexOfPage = CurrentPage;//获得当前页
ListBind();//重新将DataList绑定到数据库
}
上面的代码基本上能调用出数据库的数据,但是Datalist会把全部的数据都显示出来,分页按钮不起作用。没有财富值情高手帮忙 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询