代码如下: /// <summary>
/// 数据删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param> protected void btn_Delete_Click(object sender, EventArgs e)
{
bool result = false;
for (int i = 0; i < this.GridView1.Rows.Count;i++ )
{
int id = Convert.ToInt32(this.GridView1.DataKeys[i].Value);
if ((this.GridView1.Rows[i].Cells[0].FindControl("chk_ID") as CheckBox).Checked == true)
{
Delete(id);
result = true;
//Response.Write("删除成功!");
}
}
if(!result)
{
Response.Write("<script>alert('请选择要删除的记录!')</script>");
} BindGridView();
} /// <summary>
/// 删除的方法
/// </summary>
/// <param name="id"></param>
private void Delete(int id)
{
string strDelete = "delete from Products where ProductID=@id";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(strDelete, conn);
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
} }
如果GridView中的CheckBox被选中,则进行删除操作,可以是一条或者多条,现在的问题是好像 if 条件语句中的删除方法没有被执行,程序运行的时候,一直提示:“请选择要删除的记录!”,怎么回事呀?
if ((this.GridView1.Rows[i].Cells[0].FindControl("chk_ID") as CheckBox).Checked == true)
{
Delete(id);
result = true;
//Response.Write("删除成功!");
}
/// 数据删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param> protected void btn_Delete_Click(object sender, EventArgs e)
{
bool result = false;
for (int i = 0; i < this.GridView1.Rows.Count;i++ )
{
int id = Convert.ToInt32(this.GridView1.DataKeys[i].Value);
if ((this.GridView1.Rows[i].Cells[0].FindControl("chk_ID") as CheckBox).Checked == true)
{
Delete(id);
result = true;
//Response.Write("删除成功!");
}
}
if(!result)
{
Response.Write("<script>alert('请选择要删除的记录!')</script>");
} BindGridView();
} /// <summary>
/// 删除的方法
/// </summary>
/// <param name="id"></param>
private void Delete(int id)
{
string strDelete = "delete from Products where ProductID=@id";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(strDelete, conn);
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
} }
如果GridView中的CheckBox被选中,则进行删除操作,可以是一条或者多条,现在的问题是好像 if 条件语句中的删除方法没有被执行,程序运行的时候,一直提示:“请选择要删除的记录!”,怎么回事呀?
if ((this.GridView1.Rows[i].Cells[0].FindControl("chk_ID") as CheckBox).Checked == true)
{
Delete(id);
result = true;
//Response.Write("删除成功!");
}
是否进行DataNames的赋予???
自己断点调试吧....
{
Delete(id);
result = true;
//Response.Write("删除成功!");
}
断点这个if!看能进去不!
{
BindGridView();
}我对不起党,对不起大家!
{
}很有用,很重要的IsPostBack是Page类有一个bool类型的属性,用来判断针对当前Form的请求是第一次还是非第一次请求。当IsPostBack=true时表示非第一次请求,我们称为PostBack,当IsPostBack=false时表示第一次请求。在asp.net框架内部有很多的场景需要判断IsPostBack,比如LoadAllState等操作就需要在PostBack的时候进行
CheckBox check=(CheckBox)this.GridView1.Rows[i].Cells[0].FindControl("chk_ID");
看看check里面有没有东西,