一个实现分页浏览的程序
小弟(是个新手)写了一个关于分页浏览的程序,可是当浏览到第2页时再怎么点"下一页"它都只停留在第2页,而不管在第几页,再点"上一页时"它就只回到第1页,搞了几天了,就是找不出问题在哪里,请教各位大哥,看我的问题出在哪里?如果你们有更好的实现方法还请赐教!谢谢!
后台代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ArticleCenter
{
/// <summary>
/// DisplayArticlesByCategory 的摘要说明。
/// </summary>
public class DisplayArticlesByCategory : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnFirst;
protected System.Web.UI.WebControls.Button btnPrevious;
protected System.Web.UI.WebControls.Button btnLast;
protected System.Web.UI.WebControls.DropDownList ddPage;
protected System.Web.UI.WebControls.Label lblCurrentPage;
protected System.Web.UI.WebControls.Label lblTotalPages;
protected System.Web.UI.WebControls.Table tableArticles;
protected System.Web.UI.WebControls.Button btnNext;
ArrayList arts=new ArrayList();
private static readonly int PAGE_SIZE=3;
public int CurrentPage
{
get{return int.Parse(lblCurrentPage.Text);}
set{lblCurrentPage.Text=Convert.ToString(value);}
}
public int TotalPages
{
get{return int.Parse(lblTotalPages.Text);}
set
{
lblTotalPages.Text=Convert.ToString(value);
//因为最大页数发生改变,所以要重新设置
//下拉列表ddPages的内容
ddPage.Items.Clear();
if(value>0)
{
ddPage.Items.Add(" ");
for(int i=1;i<=value;i++)
ddPage.Items.Add(i.ToString());
}
}
}
//设置导航按钮的状态
private void EnableDisableButtons(int iTotalPages)
{
btnFirst.Enabled=(CurrentPage<=1)?false:true;
btnPrevious.Enabled=(CurrentPage<=1)?false:true;
btnNext.Enabled=(CurrentPage<iTotalPages)?true:false;
btnLast.Enabled=(CurrentPage<iTotalPages)?true:false;
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
CurrentPage=1;
if(Request.QueryString["catid"]==null) return;
arts=Yoursun.Business.ArtCategory.GetArticles(Convert.ToInt32(Request.QueryString["catid"]),-1);
int totalCount=arts.Count;
if((totalCount%PAGE_SIZE)==0) TotalPages=totalCount/PAGE_SIZE;
else TotalPages=(totalCount/PAGE_SIZE)+1;
if(TotalPages==0) CurrentPage=0;
DispArticle(arts);
EnableDisableButtons(TotalPages);
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);
this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
this.ddPage.SelectedIndexChanged += new System.EventHandler(this.ddPage_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DispArticle(ArrayList arts)
{
//添加表格头部
tableArticles.Rows.Clear();
TableRow headRow=new TableRow();
TableCell headCellImg=new TableCell();
headCellImg.Text="<img src\"images/smile.gif\">";
headCellImg.Width=20;
headRow.Cells.Add(headCellImg);
TableCell headCellTitle=new TableCell();
headCellTitle.Text="标题";
headCellTitle.Width=120;
headRow.Cells.Add(headCellTitle);
TableCell headCellDate=new TableCell();
headCellDate.Text="日期";
headCellDate.Width=120;
headRow.Cells.Add(headCellDate);
tableArticles.Rows.Add(headRow);
//形成表格内容
int startIndex=(CurrentPage-1)*PAGE_SIZE;
int endIndex=(startIndex+PAGE_SIZE>arts.Count)?arts.Count:(startIndex+PAGE_SIZE);
for(int i=startIndex;i<endIndex;i++)
{
Yoursun.Data.ArticleDetails art=(Yoursun.Data.ArticleDetails)arts[i];
TableCell cellImg=new TableCell();
cellImg.Text="<img src=\"images/文章.gif\">";
cellImg.Width=20;
TableCell cellTitle=new TableCell();
HyperLink link=new HyperLink();
link.NavigateUrl="DisplayArticle.aspx?id="+art.ID.ToString();
link.Text=art.Title;
link.Target="content";
link.CssClass="five";
cellTitle.Controls.Add(link);
TableCell cellDate=new TableCell();
cellDate.Text=art.Time.ToLongDateString();
cellDate.Width=120;
cellDate.CssClass="five";
TableRow row=new TableRow();
row.Cells.Add(cellImg);
row.Cells.Add(cellTitle);
row.Cells.Add(cellDate);
tableArticles.Rows.Add(row);
}
}
private void btnFirst_Click(object sender, System.EventArgs e)
{
if(CurrentPage>1)
{
CurrentPage=1;
DispArticle(arts);
EnableDisableButtons(TotalPages);
}
}
private void btnPrevious_Click(object sender, System.EventArgs e)
{
if(CurrentPage>1)
{
CurrentPage--;
DispArticle(arts);
EnableDisableButtons(TotalPages);
}
}
private void btnNext_Click(object sender, System.EventArgs e)
{
if(CurrentPage<TotalPages)
{
CurrentPage++;
DispArticle(arts);
EnableDisableButtons(TotalPages);
}
}
private void btnLast_Click(object sender, System.EventArgs e)
{
if(CurrentPage<TotalPages)
{
CurrentPage=TotalPages;
DispArticle(arts);
EnableDisableButtons(TotalPages);
}
}
private void ddPage_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
}
}
后台代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ArticleCenter
{
/// <summary>
/// DisplayArticlesByCategory 的摘要说明。
/// </summary>
public class DisplayArticlesByCategory : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnFirst;
protected System.Web.UI.WebControls.Button btnPrevious;
protected System.Web.UI.WebControls.Button btnLast;
protected System.Web.UI.WebControls.DropDownList ddPage;
protected System.Web.UI.WebControls.Label lblCurrentPage;
protected System.Web.UI.WebControls.Label lblTotalPages;
protected System.Web.UI.WebControls.Table tableArticles;
protected System.Web.UI.WebControls.Button btnNext;
ArrayList arts=new ArrayList();
private static readonly int PAGE_SIZE=3;
public int CurrentPage
{
get{return int.Parse(lblCurrentPage.Text);}
set{lblCurrentPage.Text=Convert.ToString(value);}
}
public int TotalPages
{
get{return int.Parse(lblTotalPages.Text);}
set
{
lblTotalPages.Text=Convert.ToString(value);
//因为最大页数发生改变,所以要重新设置
//下拉列表ddPages的内容
ddPage.Items.Clear();
if(value>0)
{
ddPage.Items.Add(" ");
for(int i=1;i<=value;i++)
ddPage.Items.Add(i.ToString());
}
}
}
//设置导航按钮的状态
private void EnableDisableButtons(int iTotalPages)
{
btnFirst.Enabled=(CurrentPage<=1)?false:true;
btnPrevious.Enabled=(CurrentPage<=1)?false:true;
btnNext.Enabled=(CurrentPage<iTotalPages)?true:false;
btnLast.Enabled=(CurrentPage<iTotalPages)?true:false;
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
CurrentPage=1;
if(Request.QueryString["catid"]==null) return;
arts=Yoursun.Business.ArtCategory.GetArticles(Convert.ToInt32(Request.QueryString["catid"]),-1);
int totalCount=arts.Count;
if((totalCount%PAGE_SIZE)==0) TotalPages=totalCount/PAGE_SIZE;
else TotalPages=(totalCount/PAGE_SIZE)+1;
if(TotalPages==0) CurrentPage=0;
DispArticle(arts);
EnableDisableButtons(TotalPages);
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);
this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
this.ddPage.SelectedIndexChanged += new System.EventHandler(this.ddPage_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DispArticle(ArrayList arts)
{
//添加表格头部
tableArticles.Rows.Clear();
TableRow headRow=new TableRow();
TableCell headCellImg=new TableCell();
headCellImg.Text="<img src\"images/smile.gif\">";
headCellImg.Width=20;
headRow.Cells.Add(headCellImg);
TableCell headCellTitle=new TableCell();
headCellTitle.Text="标题";
headCellTitle.Width=120;
headRow.Cells.Add(headCellTitle);
TableCell headCellDate=new TableCell();
headCellDate.Text="日期";
headCellDate.Width=120;
headRow.Cells.Add(headCellDate);
tableArticles.Rows.Add(headRow);
//形成表格内容
int startIndex=(CurrentPage-1)*PAGE_SIZE;
int endIndex=(startIndex+PAGE_SIZE>arts.Count)?arts.Count:(startIndex+PAGE_SIZE);
for(int i=startIndex;i<endIndex;i++)
{
Yoursun.Data.ArticleDetails art=(Yoursun.Data.ArticleDetails)arts[i];
TableCell cellImg=new TableCell();
cellImg.Text="<img src=\"images/文章.gif\">";
cellImg.Width=20;
TableCell cellTitle=new TableCell();
HyperLink link=new HyperLink();
link.NavigateUrl="DisplayArticle.aspx?id="+art.ID.ToString();
link.Text=art.Title;
link.Target="content";
link.CssClass="five";
cellTitle.Controls.Add(link);
TableCell cellDate=new TableCell();
cellDate.Text=art.Time.ToLongDateString();
cellDate.Width=120;
cellDate.CssClass="five";
TableRow row=new TableRow();
row.Cells.Add(cellImg);
row.Cells.Add(cellTitle);
row.Cells.Add(cellDate);
tableArticles.Rows.Add(row);
}
}
private void btnFirst_Click(object sender, System.EventArgs e)
{
if(CurrentPage>1)
{
CurrentPage=1;
DispArticle(arts);
EnableDisableButtons(TotalPages);
}
}
private void btnPrevious_Click(object sender, System.EventArgs e)
{
if(CurrentPage>1)
{
CurrentPage--;
DispArticle(arts);
EnableDisableButtons(TotalPages);
}
}
private void btnNext_Click(object sender, System.EventArgs e)
{
if(CurrentPage<TotalPages)
{
CurrentPage++;
DispArticle(arts);
EnableDisableButtons(TotalPages);
}
}
private void btnLast_Click(object sender, System.EventArgs e)
{
if(CurrentPage<TotalPages)
{
CurrentPage=TotalPages;
DispArticle(arts);
EnableDisableButtons(TotalPages);
}
}
private void ddPage_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
}
}