日期:2014-05-17 浏览次数:20514 次
public void DataListBind() //将数据绑定到Datalist控件
{
string connectString = @"Data Source=.;Initial Catalog=MyWeb;User ID=sa;Password=123456";
SqlConnection conn = new SqlConnection(connectString);
string sqlStr = "select * from Message order by no desc";
SqlDataAdapter sda = new SqlDataAdapter(sqlStr, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "pro");
//创建数据源
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["pro"].DefaultView;
//允许分页
pds.AllowPaging = true;
//设置每页显示记录数
pds.PageSize = 5;
//获取总页数
pageCount = pds.PageCount;
this.lb_PageCount.Text = pageCount.ToString();
pds.CurrentPageIndex = currentPage - 1;
//当前页
this.lb_CurrentPage.Text = Convert.ToString(currentPage);
//数据绑定
this.dl_ShowMessage.DataSource = pds;
this.dl_ShowMessage.DataBind();
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void PreviousLB_Click(object sender, EventArgs e)
{
if (this.lb_CurrentPage.Text != "1")
{
currentPage = int.Parse(this.lb_CurrentPage.Text) - 1;
this.lb_CurrentPage.Text = currentPage.ToString();
DataListBind();
}
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void NextLB_Click(object sender, EventArgs e)
{
if (this.lb_PageCount.Text == this.lb_CurrentPage.Text)
{
}
else
{
currentPage = int.Parse(this.lb_CurrentPage.Text) + 1;
this.lb_CurrentPage.Text = currentPage.ToString();
DataListBind();
}
}
/// <summary>
/// 第一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void FirstLB_Click(object sender, EventArgs e)
{
if (this.lb_CurrentPage.Text == "1")
{
}
else
{
currentPage = 1;
DataListBind();
}
}
/// <summary>
/// 最后一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void EndLB_Click(object sender, EventArgs e)
{
this.lb_CurrentPage.Text = this.lb_PageCount.Text;
currentPage = int.Parse(this.lb_CurrentPage.Text);
DataListBind();
}
------解决方案--------------------