求助DataList的分页问题~~~
我的程序代码如下:
就是往名为Score的DataList中加入分页
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
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;
public partial class food : System.Web.UI.Page
{
private int cPage = 2;
OleDbConnection MyConn;
int PageSize, RecordCount, PageCount, CurrentPage;
public void Page_Load(Object src, EventArgs e)
{
//设定PageSize
PageSize = cPage;
//连接语句
string MyConnString = CStatic.getStrconn();
MyConn = new OleDbConnection(MyConnString);
MyConn.Open();
//第一次请求执行
if (!Page.IsPostBack)
{
CurrentPage = 0;
ViewState[ "PageIndex "] = 0;
//计算总共有多少记录
RecordCount = CalculateRecord();
// lblRecordCount.Text = RecordCount.ToString();
//计算总共有多少页
PageCount = (RecordCount / PageSize) + 1;
ViewState[ "PageCount "] = PageCount;
ListBind();
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
int iID = 0;
try
{
iID = CStatic.GetInt(Request.QueryString[ "id "].ToString());
}
catch { }
string strCount = "select count(*) as icount from Food_Class where FID= " + iID;
OleDbCommand MyComm = new OleDbCommand(strCount, MyConn);
OleDbDataReader dr = MyComm.ExecuteReader();
if (dr.Read())
{
intCount = Int32.Parse(dr[ "icount "].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
ICollection CreateSource()
{
int StartIndex;
int iID = 0;
try
{
iID = CStatic.GetInt(Request.QueryString[ "id "].ToString());
}
catch { }
//设定导入的起终地址
StartIndex = CurrentPage * PageSize;
string strSel = "select * from Food_Class where FID= " + iID;
DataSet ds = new DataSet();
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel, MyConn);
MyAdapter.Fill(ds, StartIndex, PageSize, "Score ");
return ds.Tables[ "Score "].DefaultView;
}
public void ListBind()
{
score.DataSource = CreateSource();
score.DataBind();
//Label1.Text = CurrentPage.ToString();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if (CurrentPage > = (PageCount - 1)) lbnNextPage.Enabled = false;
if (CurrentPage <= 1) lbnPrevPage.Enabled = false;
}
public void Page_OnClick(Object sender, CommandEventArgs e)
{
CurrentPage = (int)ViewState[ "PageIndex "];
PageCount = (int)ViewState[ "PageCount "];
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch (cmd)
{
case "next ":
if (CurrentPage < (PageCount - 1)) CurrentPage++;
break;
case "prev ":
if (CurrentPage > 0) CurrentPage--;
break;
}
ViewState[ "PageIndex "] = CurrentPage;
ListBind();
}
}
错误提示如下:
“/”应用程序中的服务器错误。
--------------------------------------------
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 121: public void Page_OnClick(Object sender, CommandEventArgs e)
行 122: {
行 123: CurrentPage = (int)ViewState[ "PageIndex "];
行 124: PageCount = (int)ViewState[ "PageCount "];
行 125:
源文件: e:\www.bfspmx.com\food.aspx.cs 行: 123
[解决办法]
//计算总共有多少页
PageCount = (RecordCount / PageSize) + 1;
============================================>
如果只有2条记录,而每页显示2条记录,那么按楼主:
PageCount = (RecordCount / PageSize) + 1 //结果为2,显然是错误的
正确应该这样:PageCount = (RecordCount % PageSize == 0) ? RecordCount / PageSize : RecordCount / PageSize + 1;
[解决办法]
CurrentPage = (int)ViewState[ "PageIndex "];
PageCount = (int)ViewState[ "PageCount "];
==========>
if (ViewState[ "PageIndex "] == null)
CurrentPage = 1;
else
CurrentPage = (int)ViewState[ "PageIndex "];
PageCount也一样!
[解决办法]
XproerBBS上面有分页的源代码:
你可以下载XproerBBS,已经开源
最新源码下载地址:http://www.xproer.com/download/FoxBBS_OpenSource.rar
也可以在QQ群共享上在下载。
QQ群号:6259764
在QQ群中有两个文件
(1)FoxBBS 论坛源代码
(2)images 论坛图片文件。
使用时将它他们解压到同一个目录中,推荐解压到FoxBBS文件夹中,因为项目中许多.ASPX文件是 以FoxBBS为根目录的。例如:/FoxBBS/test.aspx
XproerBBS的安装和运行环境:
开发环境:Microsoft .NET Framework1.1
语言:C#.NET 1.1
数据库: ACCESS
简介:
XproerBBS开源项目是新手学习不可多得的资料,在项目中使用了许多目前比较流行的技术。
(1)如何操作ADO.NET,
(2)如何对数据进行分页。
(3)如何在ACCESS中使用存储过程。
(4)如何使用搜索ACCESS存储过程。
(5)如何注册用户。
(6)如何验证用户权限。
(7)如何使用AJAX技术。
这些对于刚入门或者刚开始学习使用ASP.NET(C#)开发网站的朋友来说,会提供许多方便。便于快 入学习和掌握ASP.NET(C#)技术。由于BBS论坛是基于数据库构架的,这对于想学习如何操作数据 库的朋友来说又是一大宝贝。
已包含技术:
(1)常用分页算法,上下页分页算法,这个算法是目前所有分页中效率最高的
(2)缓存技术,成熟的TxtCache技术,提高系统性能。
(3)用户身份验证技术,Session + Cookie 双重结合,极大的减轻了服务器的负担。
(4)ACCESS存储过程操作实例,此论坛基本上是建立在存储过程的基础上的,所以你可以在它的源 码里面学到操作存储过程的实例。
(5)自定义的三层构架,
(6)用户权限验证
(7)用户注册模板
(8)AJAX技术
论坛地址:www.xproer.com/bbs/index.html
说明:
由于论坛项目是使用本地的路径,所以如果下载下来后直接打开项目的话可能会出现路径错误 。所以需要将源代码放到wwwroot文件夹中的子文件夹中。
我本地设置如下
c:\wwwroot\FoxBBS\
朋友们下载后最好能建一个FoxBBS 文件夹,然后将 FoxBBS 时面的所有文件解决到 FoxBBS 文件 夹里面
下面是一些教程和一些参考资料,希望能给朋友们带来更多的帮助。
XproerBBS简介:
http://www.xproer.com/bbs/thread-3-606.aspx
XproerBBS如何后台管理:
http://www.xproer.com/bbs/thread-3-602.aspx
XproerBBS学习教程-数据库配置:
http://www.xproer.com/bbs/thread-3-605.aspx
XproerBBS文件说明:
http://www.xproer.com/bbs/thread-3-613.aspx
XproerBBS文件夹结构图:
http://www.xproer.com/bbs/thread-3-614.aspx
XproerBBS数据表结构图:
http://www.xproer.com/bbs/thread-3-617.aspx
XproerBBS存储过程:
http://www.xproer.com/bbs/thread-3-610.aspx
XproerBBS视图:
http://www.xproer.com/bbs/thread-3-611.aspx
XproerBBS存储过程调用实例:(每天10个更新)
http://www.xproer.com/bbs/thread-3-612.aspx
manage文件夹说明(含文件)
http://www.xproer.com/bbs/thread-3-622.aspx
js文件夹说明(含文件)
http://www.xproer.com/bbs/thread-3-623.aspx
Operate文件夹说明(前台管理,含文件)
http://www.xproer.com/bbs/thread-3-624.aspx
USControls文件夹说明(用户控件,含文件)
http://www.xproer.com/bbs/thread-3-625.aspx
如果你在学习和使用XproerBBS的源码过程中遇到一些困难和问题,我们建议你将问题发到http://www.xproer.com/bbs/thread.aspx?fid=3 (论坛交流区),这样便于更多的朋友详细的了解问题和帮你解答,当然我们源开发人员也会在最短的时间内回答你的问题。
[解决办法]
PagedDataSource ps = new PagedDataSource();
ps.AllowPaging = true;
ps.CurrentPageIndex = 0;
ps.DataSource = ds.Tables[0].DefaultView;
ps.PageSize = 8;
this.dl_List.DataSource = ps;
this.dl_List.DataBind();
[解决办法]
一步一步的调试啊.