我是一个入门者,最近老师流了一个练习是作留言板,把留言信息分页显示出来?我上网搜索了好久,都没有我能看懂的(可能比较笨),我只是复制老师的一段代码如下:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { int nRecCount;//定义总记录数 int pageSize=3;//定义每页显示记录数 //分页功能没有实现 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Bind(); if(Request.UrlReferrer!=null) { ViewState["PageIndex"] = Request.UrlReferrer.ToString(); } } } public int PageIndex { get { int pageIndex = 0; Object obj = ViewState["PageIndex"]; if (obj == null) return pageIndex; int.TryParse(obj.ToString(), out pageIndex); return pageIndex; } set { ViewState["PageIndex"] = value; } }
////定义下一页,遇到最后一页停止 protected void lbNext_Click(object sender, EventArgs e) { int count = (int)Application["nRecCount"]; while (PageIndex + 1 <= count / pageSize) { PageIndex++; } Bind(); return; } ////定义上一页,到首页后停止 protected void lbPre_Click(object sender, EventArgs e) { if (PageIndex <= 0) { PageIndex = 0; return; } PageIndex--; Bind(); }
protected void rptMessage_ItemCommand(object source, RepeaterCommandEventArgs e) { } public void Bind() { //SqlConnection conn = new SqlConnection(@"data source=(local)\sqlexpress;database=testdb;Integrated Security=SSPI"); string connection = ConfigurationManager.ConnectionStrings["test"].ConnectionString; SqlConnection conn = new SqlConnection(connection); string sqlStr = "select * from guestbook order by gdatetime desc"; SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlStr, conn); DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet); DataTable dataTable = dataSet.Tables[0]; nRecCount = dataTable.Rows.Count;//获得总记录数 Application["nRecCount"] = nRecCount; rptMessage.DataSource = dataTable; rptMessage.DataBind(); } } 不知道什么地方需要改动?另外有没有朋友帮忙解释一下分页原理?老师的代码(我改了一些)我不懂.非常感谢
|