新手请教问题 DataGrid 分页问题

tw_ak47 2006-10-27 03:29:28
我查询功能都是对的 分页出点问题,点第2页后没数据,再点查询后第2页数据又出来了(第3页同理)!不知道怎么解决?
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient ;
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.Configuration;

namespace addcscs
{
/// <summary>
/// cxcs 的摘要说明。
/// </summary>
public class cxcs : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Button chaxun;
protected System.Web.UI.WebControls.TextBox mc;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string strmc,strdlb;
//string conStr = ConfigurationSettings.AppSettings["connectionString"];
//strmc=mc.Text.ToString();
//strdlb=DropDownList1.SelectedItem.Value;



}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.mc.TextChanged += new System.EventHandler(this.mc_TextChanged);
this.chaxun.Click += new System.EventHandler(this.chaxun_Click);
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void mc_TextChanged(object sender, System.EventArgs e)
{

}

private void chaxun_Click(object sender, System.EventArgs e)
{
string conStr = ConfigurationSettings.AppSettings["connString"];
string strsql= "select * from Csmx where mc like '%"+mc.Text.ToString()+"%' AND dlb like '%"+DropDownList1.SelectedItem.Value+"%' ";
SqlConnection cxconn=new SqlConnection( conStr);
SqlDataAdapter cxAdapter = new SqlDataAdapter(strsql,cxconn);
cxconn.Open();
DataSet cxSet=new DataSet();
cxAdapter.Fill(cxSet,"Csmx");
DataGrid1.DataSource=cxSet;
DataGrid1.DataBind();

}



private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
DataGrid1.DataBind();
}

private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{

}


}
}
...全文
228 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsfgis 2006-11-28
  • 打赏
  • 举报
回复
谢了
jndxk 2006-11-10
  • 打赏
  • 举报
回复
mark
liln 2006-10-27
  • 打赏
  • 举报
回复
换页后,重新绑定数据源
tw_ak47 2006-10-27
  • 打赏
  • 举报
回复
呵!问题解决!今天多谢各位前辈帮忙了!
lekelaier_msn 2006-10-27
  • 打赏
  • 举报
回复
private void chaxun_Click(object sender, System.EventArgs e)
{
GetSqlData();
}

private void GetSqlData() //就是把原来chaxun_Click中的代码移进来
{
string conStr = ConfigurationSettings.AppSettings["connString"];
string strsql= "select * from Csmx where mc like '%"+mc.Text.ToString()+"%' AND dlb like '%"+DropDownList1.SelectedItem.Value+"%' ";
SqlConnection cxconn=new SqlConnection( conStr);
SqlDataAdapter cxAdapter = new SqlDataAdapter(strsql,cxconn);
cxconn.Open();
DataSet cxSet=new DataSet();
cxAdapter.Fill(cxSet,"Csmx");
DataGrid1.DataSource=cxSet;
DataGrid1.DataBind();
}

private void DataGrid1_PageIndexChanged(objectsource,System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
GetSqlData();
}

private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{

}


}
tw_ak47 2006-10-27
  • 打赏
  • 举报
回复
我先试试看!
tw_ak47 2006-10-27
  • 打赏
  • 举报
回复
我还是菜鸟!!!只有麻烦写清楚看看!!!
lekelaier_msn 2006-10-27
  • 打赏
  • 举报
回复
恩,楼上说的对,把你的 chaxun_Click 中的代码移到一个方法中,如
private void GetSqlData(){....},chaxun_Click中调用它,最后在DataGrid1_PageIndexChanged里把DataGrid1.DataBind();改为GetSqlData()就应该可以了

Robert_Wang1983 2006-10-27
  • 打赏
  • 举报
回复
订阅DataBound事件,再里面调用
string conStr = ConfigurationSettings.AppSettings["connString"];
string strsql= "select * from Csmx where mc like '%"+mc.Text.ToString()+"%' AND dlb like '%"+DropDownList1.SelectedItem.Value+"%' ";
SqlConnection cxconn=new SqlConnection( conStr);
SqlDataAdapter cxAdapter = new SqlDataAdapter(strsql,cxconn);
cxconn.Open();
DataSet cxSet=new DataSet();
cxAdapter.Fill(cxSet,"Csmx");
DataGrid1.DataSource=cxSet;
tw_ak47 2006-10-27
  • 打赏
  • 举报
回复
可以具体点吗?谢谢!!!
xingyaohua 2006-10-27
  • 打赏
  • 举报
回复
在DataGrid1_PageIndexChanged里
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;后
光DataGrid1.DataBind();不行的
得重新分配DataGrid1的数据源
icehawk 2006-10-27
  • 打赏
  • 举报
回复
点按钮一次就要重新从服务器请求页面的,这个时候DataSet都没了,当然没数据了


62,133

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧