求救~!关于分页的CurrentPageIndex的变更

penweline 2007-11-29 11:01:04
在下初学者,使用DataGrid的自带分页功能,我在主页面放有一个listbox跟数据库绑定了,选中listbox中的项能在DataGrid1中显示该选中项对应的全部信息(AutoPostBack为True).页面还有一个linkbutton按钮,点击会从同一数据库中查询该选中项的部分信息在DataGrid1中显示.请看代码:

private void BindToData() //自定义listBox绑定方法
{
SqlConnection con=DBCon.createCon(); //自定义DBCon类连接数据库
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand("select * from 数据表 where ID='"+this.ListBox1.SelectedValue+"'",con);
DataSet ds=new DataSet();
sda.Fill(ds,"sp");
this.DataGrid1.DataSource=ds.Tables["sp"];
this.DataGrid1.DataBind();
}
private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.BindToData();
}


private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
if(DataGrid1.CurrentPageIndex>DataGrid1.PageCount)
DataGrid1.CurrentPageIndex=DataGrid1.PageCount;
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
this.BindToData(); //调用ListBox绑定
}
private void LinkButton1_Click(object sender, System.EventArgs e)
{
this.DataGrid1.CurrentPageIndex=0; //归0
SqlConnection con=DBCon.createCon();
SqlDataAdapter sda1=new SqlDataAdapter();
sda1.SelectCommand=new SqlCommand("select * from 数据表 where ID='"+this.ListBox1.SelectedValue+"' and Name like '%%'",con);
DataSet ds1=new DataSet();
sda1.Fill(ds1,"sp");
this.DataGrid1.DataSource=ds1.Tables["sp"];
this.DataGrid1.DataBind();


现在的问题是:我在查询选中项的全部信息后再点击linkbutton查询选中项的部分信息(SQL查询语句不一样!),页码也正常显示,但我点击"下一页"按钮时DataGrid1的PageIndexChanged又会自动调用this.BindToData(); 从而显示全部信息...请问我该怎样避免这一现象?跪求~~
...全文
112 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
penweline 2007-11-30
  • 打赏
  • 举报
回复
请教达人,顶起了...在线等
lyg1166 2007-11-30
  • 打赏
  • 举报
回复
可以写两个绑定DataGrid的方法,根据ListBox1和LinkButton1的选择不同调用不同的方法.不知是否可以?
penweline 2007-11-30
  • 打赏
  • 举报
回复
我的问题就是:ListBox1和LinkButton1这两次查询过程的DataGrid1_PageIndexChanged事件怎么写?-_-!
lyg1166 2007-11-30
  • 打赏
  • 举报
回复
不明白什么意思!
penweline 2007-11-30
  • 打赏
  • 举报
回复
顶起来..请大家帮忙啊
penweline 2007-11-29
  • 打赏
  • 举报
回复
哎呀,好象有点文不对题了:(
请教~!

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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