我在做一个留言板,想把数据库某表中的记录逐条分页显示出来,就像csdn论坛这种样式(完了还有管理,回复等功能),请教具体步骤跟方法思路,不要贴链接,我看过好多个了,要么老长不知如何套用,麻烦前辈耐心指点一下,越详细越好了,(创建什么文件开始),在线等,弄好速度给分
------解决方案--------------------------------------------------------
分页可以用AspNetPager,示例在这里:http://webdiyer.com/AspNetPagerDemo/default.aspx
------解决方案--------------------------------------------------------
思路无非是从数据库里读取 第n条到第m条的数据。。其中这个n和m是根据你需要分页的每页条数决定的。比如你15条数据分3页显示,你要查看第二页的数据就查询第6到第10条数据。数据库查询的部分自己写存储过程或者SQL语句了,分页控件的话可以用 AspNetPager ,用法可以参考 http://blog.csdn.net/ptyzhu/article/details/7911708
------解决方案--------------------------------------------------------
我做的留言板 跟CSDN类似 有回复功能
管理吗 只有 删除 留言 删除 回复
用DataList 控件 回复的话 在DataList里面嵌套一个DataList
这是后台给DataList 绑定数据 分页代码
- C# code
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(); }
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------