CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

请问在查询结果中的分页怎么实现。高手帮忙,感激。

楼主huhang99(lock)2005-08-03 15:59:20 在 .NET技术 / ASP.NET 提问

MyCommand.Parameters.Add("@uid",   SqlDbType.NVarChar,   255).Value   =   SearchShow.Items[4].Cells[0].Text;  
  MyCommand.Parameters.Add("@uid",   SqlDbType.NVarChar,   255).Value   =   ViewState[(SearchShow.CurrentPageIndex   +   1).ToString()];  
  这两句是什么意思。  
  在查询结果中怎么实现分页,和直接读数据库的分页有哪里要注意的地方。 问题点数:50、回复次数:4Top

1 楼mapserver(杨东 http://mapserver.cnblogs.com)回复于 2005-08-03 16:07:43 得分 0

在Asp.net搜索“分页”关键字。Top

2 楼udonome(。)(。)回复于 2005-08-03 16:17:35 得分 0

上面2句话表示增加参数,一个从DataGrid中得到,一个从ViewState中得到Top

3 楼sd166(原来如此)回复于 2005-08-03 16:45:06 得分 0

得到两个参数值  
  在结果中分页可以用DataGrid的自带的分页,但影响效率,也可以用自定义分页:  
  public   void   boundgrid()  
  {  
  conn=new   SqlConnection("Server=.;uid=sa;pwd=sa;database=pubs");  
  adapter=new   SqlDataAdapter("select   *   from   employee",conn);  
  //当前显示内容  
  adapter.Fill(dataset,this.startindex,this.DataGrid1.PageSize,"CurDataTable");  
   
  adapter.Fill(dataset,"AllDataTable");  
  //定义行数大小  
  this.DataGrid1.VirtualItemCount=this.dataset.Tables["AllDataTable"].Rows.Count;  
  this.DataGrid1.DataSource=dataset.Tables["CurDataTable"];  
  this.DataGrid1.DataBind();  
   
  }  
  前提是先选中自定义分页项.  
  在数据库中分页方法很多:可以在sql语句中用top.not   in   等提取当前显示的数据,这样的例子很多,楼主可以搜一下.  
  Top

4 楼huhang99(lock)回复于 2005-08-03 17:14:43 得分 0

这个是代码,帮我改改。  
   
  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;  
  using   System.Data.SqlClient;  
   
  namespace   (local).adminback  
  {  
  ///   <summary>  
  ///   admin_index   的摘要说明。  
  ///   </summary>  
  public   class   admin_index   :   System.Web.UI.Page  
  {  
  protected   System.Web.UI.WebControls.TextBox   searchname;  
  protected   System.Web.UI.WebControls.Button   searchok;  
  protected   System.Web.UI.WebControls.Button   btnNextPage;  
  protected   System.Web.UI.WebControls.Button   btnPreviousPage;  
  protected   System.Web.UI.WebControls.DataGrid   SearchShow;  
  protected   System.Web.UI.WebControls.Label   Message;  
  int   StartIndex;  
   
   
   
  private   void   Page_Load(object   sender,   System.EventArgs   e)  
  {  
  if(searchname!=null)  
  {  
  this.btnPreviousPage.Click   +=   new   System.EventHandler(this.NavigateToPage);  
  this.btnNextPage.Click   +=   new   System.EventHandler(this.NavigateToPage);  
   
  if   (!IsPostBack)  
  {  
  StartIndex   =   0;  
   
  //   以下这段程式码用来取得数据表的数据记录数目,  
  //   并将它指派给   DataGrid   控件的   VirtualItemCount   属性。  
  SqlConnection   MyConnection   =   new   SqlConnection("server=(local);database=yp2002;user   id=sa;password=ccc");  
  SqlCommand   MyCommand   =   new   SqlCommand("select     笔数   =   COUNT(*)   from   enterprise",   MyConnection);  
  MyConnection.Open();  
   
  SqlDataReader   dr   =   MyCommand.ExecuteReader(CommandBehavior.SingleRow);  
   
  if   (dr.Read())  
  SearchShow.VirtualItemCount   =   (int)dr["笔数"];  
   
  dr.Close();  
  MyConnection.Close();  
  BindGridToSource(StartIndex,   "上一页");  
  }  
  }  
  }  
   
   
  #region   Web   窗体设计器生成的代码  
  override   protected   void   OnInit(EventArgs   e)  
  {  
  //  
  //   CODEGEN:   该调用是   ASP.NET   Web   窗体设计器所必需的。  
  //  
  InitializeComponent();  
  base.OnInit(e);  
  }  
   
  ///   <summary>  
  ///   设计器支持所需的方法   -   不要使用代码编辑器修改  
  ///   此方法的内容。  
  ///   </summary>  
  private   void   InitializeComponent()  
  {          
  this.Load   +=   new   System.EventHandler(this.Page_Load);  
   
  }  
  #endregion  
   
  private   void   searchok_Click(object   sender,   System.EventArgs   e)  
  {  
  SqlConnection   SearchConn=new   SqlConnection("server=(local);database=yp2002;user   id=sa;password=ccc");  
  string   sql="select     *   from   enterprise";  
  SqlDataAdapter   SearchAdapter=new   SqlDataAdapter(sql,SearchConn);  
   
  DataSet   SearchDS=new   DataSet();  
  SearchAdapter.Fill(SearchDS,"enterprise");  
   
   
  SearchShow.DataSource=SearchDS.Tables["enterprise"];  
  SearchShow.DataBind();  
                   
  }  
   
   
  private   void   BindGridToSource(int   StartPosition,   string   GoToPage)  
  {  
  SqlConnection   MyConnection   =   new   SqlConnection("server=(local);database=yp2002;user   id=sa;password=ccc");  
  SqlCommand   MyCommand   =   null;  
   
  switch   (GoToPage)  
  {  
  case   "上一页":  
  MyCommand   =   new   SqlCommand("SELECT   TOP   5   name   FROM   enterprise     ",   MyConnection);  
  if   (StartPosition   ==   0)  
  MyCommand.Parameters.Add("@uid",   SqlDbType.NVarChar,   255).Value   =   "";  
  else  
  MyCommand.Parameters.Add("@uid",   SqlDbType.NVarChar,   255).Value   =   ViewState[(SearchShow.CurrentPageIndex   +   1).ToString()];  
   
  break;  
   
  case   "下一页":  
  MyCommand   =   new   SqlCommand("SELECT   TOP   5   name   from   enterprise     ",   MyConnection);  
  MyCommand.Parameters.Add("@uid",   SqlDbType.NVarChar,   255).Value   =   SearchShow.Items[4].Cells[0].Text;  
   
  break;  
  }  
   
  MyConnection.Open();  
  SqlDataReader   dr   =   MyCommand.ExecuteReader();  
  SearchShow.DataSource   =   dr;  
  SearchShow.DataBind();  
  dr.Close();  
  MyConnection.Close();  
   
  ViewState[(SearchShow.CurrentPageIndex   +   1).ToString()]   =   SearchShow.Items[0].Cells[0].Text;  
  ShowPageStatus(SearchShow.VirtualItemCount);  
  }  
   
  private   void   ShowPageStatus(int   nRecords)  
  {  
  Message.Text   =   "数据源共有<b><FONT   color=   #ff0000>   "   +   nRecords   +   "   </FONT></b>笔数据记录。"   +  
  "总共有<b><FONT   color=   #ff0000>   "   +   SearchShow.PageCount   +   "   </FONT></b>页"   +   ","   +  
  "目前是第<b><FONT   color=   #ff0000>   "   +   (SearchShow.CurrentPageIndex   +   1)   +   "   </FONT></b>页。";  
  }  
   
  private   void   NavigateToPage(object   sender,   System.EventArgs   e)  
  {  
  string   PageInfo   =   ((Button)sender).CommandName;  
  switch   (PageInfo)  
  {  
  case   "上一页":  
  if   (SearchShow.CurrentPageIndex   >   0)  
  SearchShow.CurrentPageIndex   -=   1;  
  else  
  btnPreviousPage.Enabled=false;  
   
  break;  
   
  case   "下一页":  
  if   (SearchShow.CurrentPageIndex   <   (SearchShow.PageCount   -   1))  
  SearchShow.CurrentPageIndex   +=   1;  
  else  
  return;  
   
  break;  
  }  
   
  StartIndex   =   SearchShow.CurrentPageIndex   *   SearchShow.PageSize;  
  //   重新绑定至数据来源  
  BindGridToSource(StartIndex,   PageInfo);  
  }  
   
   
  }  
  }  
  Top

相关问题

  • 如何实现分页查询?
  • 在oracle如何实现分页查询?
  • 不能实现的分页查询?
  • 怎么用DB2实现分页查询??
  • 关于使用hibernate实现分页查询的问题
  • 查询后分页
  • 有查询优化经验请进:怎么实现分页显示?
  • 数据库查询分页技术实现,java 类 Vector() 在哪个包下?
  • 各位兄弟,姐妹 怎么样实现两个表查询后的分页
  • 100分求解Select语句实现分页查询数据的问题

关键词

  • asp.net
  • 代码
  • datagrid
  • 数据
  • sa
  • searchshow
  • 分页
  • virtualitemcount
  • mycommand
  • myconnection

得分解答快速导航

  • 帖主:huhang99

相关链接

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

广告也精彩

反馈

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