可以给你的checkbox加上一个js方法
用来把选中的ID,放到一个页面的runat server的Hidden里面(用某个特殊符号格开)然后在你的数据绑定事件里面
判断当前行的ID是否在这个Hidden里面然后来设置checkBox是选中当然,你也可以在翻页之前,先遍历一下你的grid的item,记录下来选中行的ID,存放在session里面然后在你的数据绑定事件里面
判断当前行的ID是否在这个Hidden里面然后来设置checkBox是选中都是一样可以完成这个功能
用来把选中的ID,放到一个页面的runat server的Hidden里面(用某个特殊符号格开)然后在你的数据绑定事件里面
判断当前行的ID是否在这个Hidden里面然后来设置checkBox是选中当然,你也可以在翻页之前,先遍历一下你的grid的item,记录下来选中行的ID,存放在session里面然后在你的数据绑定事件里面
判断当前行的ID是否在这个Hidden里面然后来设置checkBox是选中都是一样可以完成这个功能
public DataSet GetZcbd()
{
try
{
DataSet ds=new DataSet();
string searchString="select id,yy,bj from zc";
da=new OleDbDataAdapter(searchString,conn);
da.Fill(ds,"yy");
return ds;
}
catch
{
return null;
}
} //绑定DataGrid
private void BindGrid()
{
DataSet ds = new DataSet();
ds = us.GetZcbd();
if (ds!=null)
{
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
}
else
{
msg.Alert("加载数据错误!",Page);
}
}//删除数据库纪录函数
public string DeleteZcbd(int bdID)
{ int count = this.IfExiseZysx(bdID);//不必理会次句,默认count=1
if (count <= 0) return "false";
else
{
string sqlStr = "delete from zcwhere id="+bdID;
OleDbCommand cmd = new OleDbCommand(sqlStr,conn); conn.Open(); try
{
cmd.ExecuteNonQuery();
return "true";
}
catch(Exception e)
{
return e.Message.ToString();
}
finally
{
conn.Close();
}
}
}// DataGrid1_DeleteCommand事件修改函数
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int bdID = int.Parse(DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString());
string isDel = us.DeleteZcbd(bdID);
int CurrentPage = 0;
if (isDel == "true")
{
if(this.DataGrid1.CurrentPageIndex == this.DataGrid1.PageCount -1)
{
if (this.DataGrid1.CurrentPageIndex == 0)
{
this.DataGrid1.CurrentPageIndex = this.DataGrid1.PageCount -1;
}
else
{
if (this.DataGrid1.Items.Count % this.DataGrid1.PageSize == 1)
{
CurrentPage = 2;
}
else
{
CurrentPage = 1;
}
this.DataGrid1.CurrentPageIndex = this.DataGrid1.PageCount - CurrentPage;
}
}
this.BindGrid();
}
else
{
msg.Alert("删除数据错误!",Page);
}
}
注释:msg为一个类似WinForm的messagebox对话框,不必理会。可以使用label.Text代替
声明:作者Blog:http://blog.csdn.net/niwniwniw/