积极答复者
关于关于一个删除按钮的问题

问题
-
protected void Button1_Click(object sender, EventArgs e){for (int i = 0; i <= GridView1.Rows.Count - 1; i++){CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");if (cbox.Checked == true){string sqlStr = "delete from Inventory where Code='" + Convert.ToUInt32(GridView1.DataKeys[i].Value) + "'";GetCon.ExecuteSql(sqlStr);}else{Response.Write(string.Format(GetRedirect.WINALERT,"你没有选择要删除的记录!"));Response.Write(string.Format(GetRedirect.REDIRECT,"InventoryManager.aspx"));}}bindToGridView();}这是一个删除按钮触发的事件,可当我选中了一个checkbox后,它还是显示"你没有选择要删除的记录!",是不是上面的判断逻辑有问题还是其他的错误,请执教。
- 已移动 孟宪会Moderator 2009年7月25日 13:09 (发件人:.NET Framework 一般性问题讨论区)
答案
-
protected void Button1_Click(object sender, EventArgs e)
{
bool result = false;
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox cb1 = (CheckBox)gvr.FindControl("CheckBox1");
if (cb1.Checked)
{
string clientId = gvr.Cells[0].Text;
string operators = DropDownList1.SelectedValue;
result = ClientAccess.SetOperator(clientId, operators);
}
}
if (result)
{
Response.Write("<script>alert('添加成功!');</script>");
PopulateControls();
}
else
{
Response.Write("<script>alert('添加失败!');</script>");
}
}
jon.valett@gmail.com- 已标记为答案 KeFang Chen 2009年7月27日 2:46
-
protected void Button1_Click(object sender, EventArgs e) { bool fHasDeleted = false; for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); if (cbox.Checked == true) { string sqlStr = "delete from Inventory where Code='" + Convert.ToUInt32(GridView1.DataKeys[i].Value) + "'"; GetCon.ExecuteSql(sqlStr); fHasDeleted = true; } } if (!fHasDeleted) { Response.Write(string.Format(GetRedirect.WINALERT, "你没有选择要删除的记录!")); Response.Write(string.Format(GetRedirect.REDIRECT, "InventoryManager.aspx")); } bindToGridView(); }
知识改变命运,奋斗成就人生!- 已建议为答案 Raymond TangModerator 2009年7月25日 5:41
- 已标记为答案 KeFang Chen 2009年7月27日 2:46
全部回复
-
protected void Button1_Click(object sender, EventArgs e) { bool fHasDeleted = false; for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); if (cbox.Checked == true) { string sqlStr = "delete from Inventory where Code='" + Convert.ToUInt32(GridView1.DataKeys[i].Value) + "'"; GetCon.ExecuteSql(sqlStr); fHasDeleted = true; } } if (!fHasDeleted) { Response.Write(string.Format(GetRedirect.WINALERT, "你没有选择要删除的记录!")); Response.Write(string.Format(GetRedirect.REDIRECT, "InventoryManager.aspx")); } bindToGridView(); }
知识改变命运,奋斗成就人生!- 已建议为答案 Raymond TangModerator 2009年7月25日 5:41
- 已标记为答案 KeFang Chen 2009年7月27日 2:46
-
protected void Button1_Click(object sender, EventArgs e)
{
bool result = false;
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox cb1 = (CheckBox)gvr.FindControl("CheckBox1");
if (cb1.Checked)
{
string clientId = gvr.Cells[0].Text;
string operators = DropDownList1.SelectedValue;
result = ClientAccess.SetOperator(clientId, operators);
}
}
if (result)
{
Response.Write("<script>alert('添加成功!');</script>");
PopulateControls();
}
else
{
Response.Write("<script>alert('添加失败!');</script>");
}
}
jon.valett@gmail.com- 已标记为答案 KeFang Chen 2009年7月27日 2:46