我用FOR循环遍历检索Girdview中CheckBox是否被选中,但是无法实现!开始我以为是语句错误,然后我用response.write()返回搜索结果,但是我选中的CheckBox的Checked状态都为False这是怎么回事,但是我想了好久,逻辑方面没有什么问题啊!遍历的语法也没有用错.请高手指教
protected void Button1_Click(object sender, EventArgs e)
{
all.DCommd=new System.Data.OleDb.OleDbCommand();
int GridRows = Convert.ToInt32(GridView1.Rows.Count);
int i;
for (i = 0; i < GridRows; i++)
{
int Count = Convert.ToInt32(all.MySet.Tables[DataStr_1].Rows[i].ItemArray[0]);
String Command;
if (((CheckBox)GridView1.Rows[i].FindControl("DelCheck")).Checked)
{
Command = "DELETE FROM LeText WHERE LeText_ID=" + Convert.ToString(Count);
Select.DC(Command, all.MyConn);
all.MyConn.Open();
all.DCommd.ExecuteNonQuery();
all.MyConn.Close();
}
}
}这是原代码!
这个是原来数据库的代码!
这个才是控件状态搜索的代码!
all.DCommd=new System.Data.OleDb.OleDbCommand();
int GridRows = Convert.ToInt32(GridView1.Rows.Count);
int i;
for (i = 0; i < GridRows; i++)
{
int Count = Convert.ToInt32(all.MySet.Tables[DataStr_1].Rows[i].ItemArray[0]);
String Command;
bool isChecked = ((CheckBox)GridView1.Rows[i].FindControl("DelCheck")).Checked;
Response.Write(Convert.ToString(isChecked));
}返回的状态全部是FALSE,不管你选中还是没选中!
试试这个看看,打断点跑一遍,看看数组里都是些什么值
string[] deleteids = new string[GridView1.Rows.Count];
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox) GridView1.Rows[i].Cells[0].Controls[1]; //Cell[0]这个代表CheckBox列在第一列,视具体情况定
if (cb.Checked)
{
deleteids[i] = ids[i];
}
}