gridview里的checkbox问题
gridview里显示一列标题列,点击标题列字段,数据表其他内容就显示在TEXBOX里,为什么加上CHEKBOX,删除功能就不起作用了呢,如果只是显示标题列,不做数据表内容显示在TEXBOX这个功能,就能成功删除,逐句调试时,发现是string sql = "delete from news where biaoti =“”为空,是什么原因呢?
protected void Page_Load(object sender, EventArgs e)
{
string conn = System.Configuration.ConfigurationManager.AppSettings["strConn"].ToString();
SqlConnection con = new SqlConnection(conn);
con.Open();
DataSet myDataSet = new DataSet();
SqlDataAdapter myCommand = new SqlDataAdapter("newsList", con);
//填充DataSet
myCommand.Fill(myDataSet);
//关闭链接
GridView1.DataSource = myDataSet;
//绑定
GridView1.DataBind();
Button1.Attributes.Add("onclick", "return confirm('您确定要删除所有选定记录吗?');");
}
protected void Button1_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i < this.GridView1.Rows.Count; i++)
{
if (((CheckBox)this.GridView1.Rows[i].Cells[1].FindControl("CheckBox1")).Checked == true)
{
string conn = System.Configuration.ConfigurationManager.AppSettings["strConn"].ToString();
SqlConnection con = new SqlConnection(conn);
con.Open();
DataSet ds = new DataSet();
string sql = "delete from news where biaoti ='" + this.GridView1.Rows[i].Cells[1].Text.ToString() + "'";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql, con);
da.Fill(ds, "news");
con.Close();
}
}
string conn1 = System.Configuration.ConfigurationManager.AppSettings["strConn"].ToString();
SqlConnection con1 = new SqlConnection(conn1);
con1.Open();
DataSet myDataSet = new DataSet();
SqlDataAdapter myCommand = new SqlDataAdapter("newsList", con1);
//填充DataSet
myCommand.Fill(myDataSet);
//关闭链接
GridView1.DataSource = myDataSet;
//绑定
GridView1.DataBind();
con1.Close();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = GridView1.SelectedDataKey["biaoti"].ToString();
TextBox2.Text = GridView1.SelectedDataKey["fabuzhe"].ToString();
TextBox3.Text = GridView1.SelectedDataKey["shijian"].ToString();
TextBox4.Text = GridView1.SelectedDataKey["neirong"].ToString();
}
}