日期:2014-05-20  浏览次数:20383 次

各位,帮看看我用DataList图片分页的例子,错哪了?
我照一个例子做的,怎么点下一页和最后一页时出错了,我trace了一下,发现点击下一页和最后一页时,页码nPhotoCurrentPage没有增1,而仍是0,不知哪错了,哪位帮看看,谢。

                                    private   const   int   PageCount=6;   //每页显示的照片数量
private   static   int   nPhotoCount=0;//所有照片数量
private   int     nPhotoAllPageCount=0;//最大页码值
private   int   nPhotoCurrentPage=0;//当前页码
private   void   Page_Load(object   sender,   System.EventArgs   e)
{


if(!Page.IsPostBack)
{
BindPhoto();
this.lblCurrentPage.Text=(nPhotoCurrentPage+1).ToString();
}
private   void   BindPhoto()
{
SqlConnection   con=DB.CreateConnection();
con.Open();
SqlCommand   cmd=con.CreateCommand();
//获取照片数量
cmd.CommandText= "select   count(*)   from   photos ";
nPhotoCount=Convert.ToInt32(cmd.ExecuteScalar());
//获取页码数
nPhotoAllPageCount=nPhotoCount/PageCount;
if(nPhotoAllPageCount*PageCount <nPhotoCount)
{
nPhotoAllPageCount++;
}
this.lblTotalPic.Text=nPhotoCount.ToString();
this.lblTotalPage.Text=nPhotoAllPageCount.ToString();

DataSet   ds=GetPhotos(nPhotoCurrentPage*PageCount,PageCount);
this.DataList1.DataSource=ds;
this.DataList1.DataBind();

}

//获取要显示的照片
public   DataSet   GetPhotos(int   nStart,int   nMaxCount)
{
SqlConnection   con=DB.CreateConnection();
SqlDataAdapter   sda=new   SqlDataAdapter( "Pr_GetAll ",con);
sda.SelectCommand.CommandType=CommandType.StoredProcedure;
DataSet   ds=new   DataSet();
con.Open();
sda.Fill(ds,nStart,nMaxCount, "photos ");
if(con.State==ConnectionState.Open)
{
con.Close();
}
return   ds;

}

private   void   Page_Click(object   sender,System.EventArgs   e)
{
String   commandArg=((LinkButton)sender).CommandArgument;
switch(commandArg)
{
case   "First ":   nPhotoCurrentPage=0;break;
case   "Prev ":     nPhotoCurrentPage=(int)Math.Max(0,nPhotoCurrentPage-1);break;
case   "Next ":     nPhotoCurrentPage=nPhotoCurrentPage+1;break;
case   "Last ":     nPhotoCurrentPage=nPhotoAllPageCount-1;break;
default:break;
}
ViewState[ "PhotoCurrentPage "]=nPhotoCurrentPage.ToString();
BindPhoto();
this.lblCurrentPage.Text=(nPhotoCurrentPage+1).ToString();

}

------解决方案--------------------
怎么可能不是0呢。当页面装载的时候,由于你有if(!Page.IsPostBack),那么那里的代码不可能执行。然后,就直接执行到Page_Click了,当然是0。
------解决方案--------------------
自从公司要求必须使用HTML控件以后,服务器控件的使用差不多忘光了~~只有分页的还在用吴旗娃的那个ASPNETPAGER 4.X.X的版本