public void btn_Delete_Click(object sender, EventArgs e) {
for (int n = 0; n < this.Datagrid1.Items.Count; n++) { CheckBox cb = (CheckBox)Datagrid1.Items[n].FindControl("chkDel"); if (cb.Checked) { int id = int.Parse(Datagrid1.DataKeys[Datagrid1.Items[n].ItemIndex].ToString()); string strCnd = "DELETE FROM [QuestionInfo7] where Id=" + id + ""; share.exc_sql(strCnd);
} } }结果还是一样,设置断点追踪, if (mychk.Checked == true) 代码始终没从这句执行进去,mychk.Checked 的值始终为false
public void btn_Delete_Click(object sender, EventArgs e) { for (int n = 0; n < this.Datagrid1.Items.Count; n++) { CheckBox cb = (CheckBox)Datagrid1.Items[n].FindControl("chkDel"); if (cb.Checked) { int id = int.Parse(Datagrid1.DataKeys[Datagrid1.Items[n].ItemIndex].ToString()); string strCnd = "DELETE FROM [tableA] where Id=" + id + ""; exc_sql(strCnd);
} } } 结果还是一样,设置断点追踪, if (cb.Checked) 代码始终没从这句执行进去,cb.Checked的值始终为false
{
//下面这个加了吗
if (!IsPostBack)
{
}
}
2、DataGrid1.DataKeys[DataGrid1.Items[i].ItemIndex].ToString()得到你当前选中的DataGrid的行。(你先要设置DataKeys)
把CheckBox的Value设为你的数据库的主键字段,并将AutoPostBack设为True,回发后根据该CheckBox的Value值即可以确定用户选择了哪一行记录,然后录入当前时间值即可。
这个当然有,你是说
if (!IsPostBack)
{
}
要在这里面写些什么代码吗?
public void btn_Delete_Click(object sender, EventArgs e)
{
for (int n = 0; n < this.Datagrid1.Items.Count; n++) { CheckBox cb = (CheckBox)Datagrid1.Items[n].FindControl("chkDel"); if (cb.Checked)
{
int id = int.Parse(Datagrid1.DataKeys[Datagrid1.Items[n].ItemIndex].ToString()); string strCnd = "DELETE FROM [QuestionInfo7] where Id=" + id + "";
share.exc_sql(strCnd);
}
}
}结果还是一样,设置断点追踪,
if (mychk.Checked == true)
代码始终没从这句执行进去,mychk.Checked 的值始终为false
如果你把要在页面呈现的数据用客户端控件显示并用手写form提交服务器,就可以避免这个问题。
我最终选择了用mvc做类似的事,看你自己了。
public void btn_Delete_Click(object sender, EventArgs e)
{
for (int n = 0; n < this.Datagrid1.Items.Count; n++) { CheckBox cb = (CheckBox)Datagrid1.Items[n].FindControl("chkDel"); if (cb.Checked)
{
int id = int.Parse(Datagrid1.DataKeys[Datagrid1.Items[n].ItemIndex].ToString()); string strCnd = "DELETE FROM [tableA] where Id=" + id + "";
exc_sql(strCnd);
}
}
}
结果还是一样,设置断点追踪,
if (cb.Checked)
代码始终没从这句执行进去,cb.Checked的值始终为false
这个方法是获取值是CheckBox的Value设为你的数据库的主键字段,这个直接得到的是主键值啊?
DataGrid绑定数据的只要在
if (!IsPostBack)
{
}
就能获取到 CheckBox mychk = (CheckBox)this.Datagrid1.Items[i].FindControl("chkDel");
if (mychk.Checked)这个的值。
我写的GridView批量删除的:
<asp:GridView ID="myGrid" runat="server" BackColor="White" BorderColor="#CC9966"
BorderStyle="None" BorderWidth="1px" CellPadding="4" AutoGenerateColumns="False"
DataKeyNames="Id" Width="100%">
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ChkB" runat="server" />
</ItemTemplate>
<ItemStyle Width="10px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
cs:
string ids = "";
for (int i = 0; i < myGrid.Rows.Count; i++)
{
CheckBox cb = (CheckBox)(myGrid.Rows[i].Cells[0].Controls[1]);
if (cb.Checked == true)
{
string code = myGrid.DataKeys[i].Value.ToString();
ids += "" + code + ","; Label lblAttFile = (Label)myGrid.Rows[i].FindControl("lblAttFile"); if (!String.IsNullOrEmpty(lblAttFile.Text))
{
string strPath = Server.MapPath("..\\..\\Uploadloads\\" + lblAttFile.Text);
if (File.Exists(strPath))
{
File.Delete(strPath);
}
} }
}
if (ids.EndsWith(","))
{
ids = ids.Substring(0, ids.Length - 1);
}
//选择为空
if (ids == String.Empty)
{
Response.Write("<script language=javascript>alert('请选择您要删除的项后再点击删除!');</script>");
}
else
{
lib.Delete(ids);//传ids过去执行删除,ids的值类似:1,2,3,4;删除的sql是
DELETE FROM [tableA] where Id in ("+ids+");一条sql执行删除即可,避免重复打开数据库多次执行造成志愿浪费。
}
前面已经说过了,checkbox的AutoPostBack不能设置为True的,设置为true之后就不能连续勾选多个checkbox了
在
if (!IsPostBack)
{
}
里绑定DataGrid数据??
if (!IsPostBack)
{
DataSet ds =………………//获取数据
myGrid.DataSource = ds;//绑定数据
myGrid.DataBind();
}
里绑定DataGrid数据。