CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

拜请高手指点~~C#.NET中遇到的分页问题

楼主louiseautumn()2006-06-04 00:30:45 在 .NET技术 / C# 提问

我做了个留言版但是不能实现分页  
  这个是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()  
   
     
  问题点数:20、回复次数:4Top

1 楼louiseautumn()回复于 2006-06-04 00:32:38 得分 0

哟~~LeavewordDB.cs的设计粘贴掉了代码:如下  
  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();  
  }Top

2 楼cheery_ke(賀連霜冷)回复于 2006-06-04 02:05:57 得分 0

SqlDataReader   并没有实现ICollection   接口  
   
  最简单的改动为   把SqlDataReader   改成DataSet,然后绑定到具体表的View……Top

3 楼Firestone2003(笨笨小猪)回复于 2006-06-04 11:12:33 得分 0

public   ProductResults[]   GetList(string   catid,   int   currentPage,   int   pageSize,   ref   int   numResults)    
  {  
  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;   }      
  }  
  }  
   
  Top

4 楼louiseautumn()回复于 2006-06-04 13:08:35 得分 0

Thanks   very   much!Top

相关问题

关键词

得分解答快速导航

  • 帖主:louiseautumn

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo