我做了个留言版但是不能实现分页
这个是Leaveword.aspx.cs设计代码
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SureBtn.Click += new System.EventHandler(this.SureBtn_Click);
this.myDataGrid.SelectedIndexChanged += new System.EventHandler(this.myDataGrid_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
private void BindLeavewordData()
{
///获取留言的数据
LeavewordDB word = new LeavewordDB();
SqlDataReader recw = word.GetLeavewords(); ///绑定DataGrid控件的数据
myDataGrid.DataSource = recw;
myDataGrid.DataBind(); ///关闭数据读取器
recw.Close();
}
private void SureBtn_Click(object sender, System.EventArgs e)
{
LeavewordDB word = new LeavewordDB();
if(Body.Text.Length > 0)
{
///添加新的留言
word.AddLeaveword(Title.Text.Trim(),Body.Text.Trim()); ///重新绑定留言版的数据
BindLeavewordData();
}
} public String FormatBody(String sBody)
{
return(sBody.Replace("\n","<br>"));
}
这个是LeavewordDB.cs的设计代码:
当 AllowPaging 设置为真并且选定的数据源不实现 ICollection 时,AllowCustomPaging 必须为真,并且 ID 为 myDataGrid 的 DataGrid 必须设置 VirtualItemCount。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Web.HttpException: 当 AllowPaging 设置为真并且选定的数据源不实现 ICollection 时,AllowCustomPaging 必须为真,并且 ID 为 myDataGrid 的 DataGrid 必须设置 VirtualItemCount。源错误:
行 68: ///绑定DataGrid控件的数据
行 69: myDataGrid.DataSource = recw;
行 70: myDataGrid.DataBind();
行 71:
行 72: ///关闭数据读取器
源文件: f:\音乐网站\leaveword.aspx.cs 行: 70 堆栈跟踪:
[HttpException (0x80004005): 当 AllowPaging 设置为真并且选定的数据源不实现 ICollection 时,AllowCustomPaging 必须为真,并且 ID 为 myDataGrid 的 DataGrid 必须设置 VirtualItemCount。]
System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource)
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
xwq_music_net.Leaveword.BindLeavewordData() in f:\音乐网站\leaveword.aspx.cs:70
xwq_music_net.Leaveword.Page_Load(Object sender, EventArgs e) in f:\音乐网站\leaveword.aspx.cs:36
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
这个是Leaveword.aspx.cs设计代码
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SureBtn.Click += new System.EventHandler(this.SureBtn_Click);
this.myDataGrid.SelectedIndexChanged += new System.EventHandler(this.myDataGrid_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
private void BindLeavewordData()
{
///获取留言的数据
LeavewordDB word = new LeavewordDB();
SqlDataReader recw = word.GetLeavewords(); ///绑定DataGrid控件的数据
myDataGrid.DataSource = recw;
myDataGrid.DataBind(); ///关闭数据读取器
recw.Close();
}
private void SureBtn_Click(object sender, System.EventArgs e)
{
LeavewordDB word = new LeavewordDB();
if(Body.Text.Length > 0)
{
///添加新的留言
word.AddLeaveword(Title.Text.Trim(),Body.Text.Trim()); ///重新绑定留言版的数据
BindLeavewordData();
}
} public String FormatBody(String sBody)
{
return(sBody.Replace("\n","<br>"));
}
这个是LeavewordDB.cs的设计代码:
当 AllowPaging 设置为真并且选定的数据源不实现 ICollection 时,AllowCustomPaging 必须为真,并且 ID 为 myDataGrid 的 DataGrid 必须设置 VirtualItemCount。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Web.HttpException: 当 AllowPaging 设置为真并且选定的数据源不实现 ICollection 时,AllowCustomPaging 必须为真,并且 ID 为 myDataGrid 的 DataGrid 必须设置 VirtualItemCount。源错误:
行 68: ///绑定DataGrid控件的数据
行 69: myDataGrid.DataSource = recw;
行 70: myDataGrid.DataBind();
行 71:
行 72: ///关闭数据读取器
源文件: f:\音乐网站\leaveword.aspx.cs 行: 70 堆栈跟踪:
[HttpException (0x80004005): 当 AllowPaging 设置为真并且选定的数据源不实现 ICollection 时,AllowCustomPaging 必须为真,并且 ID 为 myDataGrid 的 DataGrid 必须设置 VirtualItemCount。]
System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource)
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
xwq_music_net.Leaveword.BindLeavewordData() in f:\音乐网站\leaveword.aspx.cs:70
xwq_music_net.Leaveword.Page_Load(Object sender, EventArgs e) in f:\音乐网站\leaveword.aspx.cs:36
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
public class LeavewordDB
{
private readonly string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();
public SqlDataReader GetLeavewords()
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetLeavewords",myConnection); //定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure; SqlDataReader dr = null; try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
} try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
} //返回 dr
return dr;
} public SqlDataReader GetSingleLeaveword(int nLeavewordID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_GetSingleLeaveword",myConnection); //定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure; //添加储存过程的参数
SqlParameter parameterLeavewordID = new SqlParameter("@LeavewordID",SqlDbType.Int,4);
parameterLeavewordID.Value = nLeavewordID;
myCommand.Parameters.Add(parameterLeavewordID); SqlDataReader dr = null; try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
} try
{
//执行数据库的存储过程(访问数据库)
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
} //返回 dr
return dr;
} public int AddLeaveword(String sTitle,String sBody)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_AddLeaveword",myConnection); //定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure; //创建访问数据库的参数
SqlParameter parameterTitle = new SqlParameter("@Title",SqlDbType.VarChar,100);
parameterTitle.Value = sTitle;
myCommand.Parameters.Add(parameterTitle); SqlParameter parameterBody = new SqlParameter("@Body",SqlDbType.VarChar,2000);
parameterBody.Value = sBody;
myCommand.Parameters.Add(parameterBody); SqlParameter parameterLeavewordID = new SqlParameter("@LeavewordID",SqlDbType.Int,4);
parameterLeavewordID.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(parameterLeavewordID); try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ex)
{
throw new Exception("数据库连接失败!",ex);
} try
{
//执行数据库的存储过程(访问数据库)
myCommand.ExecuteNonQuery();
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
finally
{
if (myConnection.State == ConnectionState.Open)
{
//关闭数据库的连接
myConnection.Close();
}
} return (int)parameterLeavewordID.Value;
} public void DeleteLeaveword(int nLeavewordID)
{
//定义数据库的Connection and Command
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand("Pr_DeleteLeaveword",myConnection); //定义访问数据库的方式为存储过程
myCommand.CommandType = CommandType.StoredProcedure; //创建访问数据库的参数
SqlParameter parameterLeavewordID = new SqlParameter("@LeavewordID",SqlDbType.Int,4);
parameterLeavewordID.Value = nLeavewordID;
myCommand.Parameters.Add(parameterLeavewordID); try
{
//打开数据库的连接
myConnection.Open();
}
catch(Exception ec)
{
throw new Exception("数据库连接失败!",ec);
} try
{
//执行数据库的存储过程(访问数据库)
myCommand.ExecuteNonQuery();
}
catch(Exception er)
{
throw new Exception(er.Message,er);
}
finally
{
if(myConnection.State == ConnectionState.Open)
{
//关闭数据库的连接
myConnection.Close();
}
{
numResults = 0;
int index=0;
SqlDataReader reader = GetList(catid);
ProductResults[] results = new ProductResults[pageSize];
// now loop through the list and pull out items of the specified page
int start = (int)((currentPage - 1) * pageSize);
if (start <= 0) start = 1; // skip
for (int i = 0; i < start - 1; i++) {
if (reader.Read()) numResults++;
}
if (start > 1) reader.Read(); // read the data we are interested in
while (reader.Read()) {
if (index < pageSize) {
results[index] = new ProductResults();
results[index].productid = reader.GetString(0);
results[index].name = reader.GetString(1);
index++;
}
numResults++;
} reader.Close(); // see if need to redim array
if (index == pageSize)
return results;
else {
// not a full page, redim array
ProductResults[] results2 = new ProductResults[index];
Array.Copy(results, results2, index);
return results2;
}
}注意到currentPage和pageSize了吗?原来在这里就进行了数据分页,只返回满足需要的最少的数据量,而不是象我们很多喜欢偷懒的人一样,简单的将整个DataTable一股脑的绑定到DataGrid,造成大量的数据冗余。在这里,数据被真正的读出来,并且被手动填充到一个自定义的对象数组中,我们来看看这个数组的定义:public class ProductResults
{
private string m_productid;
private string m_name; // product props
public string productid {
get { return m_productid; }
set { m_productid = value; }
} public string name {
get { return m_name; }
set { m_name = value; }
}
}