求救~!关于分页的CurrentPageIndex的变更
在下初学者,使用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(); 从而显示全部信息...请问我该怎样避免这一现象?跪求~~