请问在查询结果中的分页怎么实现。高手帮忙,感激。
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




