在Repeater中选中多个checkbox,然后点击删除按钮.如何循环删除?以下是部分代码..
for (int i = 0; i < Repeater.Items.Count; i++)
{
CheckBox cb = (CheckBox)engineer_Repeater.Items[i].FindControl("checkbox1");
if (cb.Checked)
{
Label lb = (Label)engineer_Repeater.Items[i].FindControl("label1");
}
}
for (int i = 0; i < Repeater.Items.Count; i++)
{
CheckBox cb = (CheckBox)engineer_Repeater.Items[i].FindControl("checkbox1");
if (cb.Checked)
{
Label lb = (Label)engineer_Repeater.Items[i].FindControl("label1");
}
}
{
CheckBox cb = (CheckBox)engineer_Repeater.Items[i].FindControl("checkbox1");
if (cb.Checked)
{
Label lb = (Label)Repeater.Items[i].FindControl("label1");
}
}
delete table where id in(主键)
把要删除的主键取出来中间用逗号分开
delete table where id in(主键)===============================说的不错,你现在只要把需要删除的那些 ID取出来 组成'aa','bb','cc'的字符串 然后就可以执行了
再执行sql语句delete from table where id in(变量)
{
CheckBox cb = (CheckBox)engineer_Repeater.Items[i].FindControl("checkbox1");
if (cb.Checked)
{
Label lb = (Label)engineer_Repeater.Items[i].FindControl("label1");
// 可以先隐藏删除了的行
Repeater.Items[i].Visiable = false;
}
}
{
bool flag = false;
for(int i = 0;i<Grid.Items.Count;i++)
{
CheckBox check = (CheckBox)Grid.Items[i].FindControl("cb");
if (check.Checked)
{
string id = Grid.Items[i].Cells[0].Text;
User.Id = int.Parse(id.ToString());
User.AdminDelUser();
flag = true;
}
}
chkSelect.Checked = false;
if (flag)
{
Response.Write("<script>alert('删除成功');window.location.href='"+this.Request.RawUrl+"';</script>");
if (Grid.CurrentPageIndex>0)
Grid.CurrentPageIndex--;
BindGrid();
}
else
Response.Write("<script>alert('没有选择要删除的数据');</script>"); }
int ID = 0;
foreach(DataGridItem dgItem in this.DataGrid1.Items)
{
chkID = (CheckBox)dgItem.FindControl("RowSelecter");
if(chkID.Checked)
{
ID=(int)this.DataGrid1.DataKeys[(int)dgItem.ItemIndex];
DataAccess.ExcuteCmd("DELETE FROM GroupContact WHERE id="+ID);
}
}
delete table where id in(主键)
------------------------------
怎样取?我只能取到一个,如果选的多条记录,怎么取?
ArrayList list = new ArrayLIst();
for (int i = 0; i < Repeater.Items.Count; i++)
{
CheckBox cb = (CheckBox)engineer_Repeater.Items[i].FindControl("checkbox1");
if (cb.Checked)
{
Label lb = (Label)Repeater.Items[i].FindControl("label1");
list.Add(lb.Text);
}
}string[] strDel = list.ToArray(typeof(string)) as String[];
if(strDel.length > 0){
sql = string.Format(sql,string.Join(",",strDel));
//执行sql
}
delete table where id in(主键)
------------------------------
怎样取?我只能取到一个,如果选的多条记录,怎么取?
===============================================
把主键设为DataKeyField
string vStr="";
for (int i = 0; i < Repeater.Items.Count; i++)
{
CheckBox cb = (CheckBox)engineer_Repeater.Items[i].FindControl("checkbox1");
if (cb.Checked)
{
vStr.Append(engineer_Repeater.DataKeys[i].ToString());
vStr.Append(",");Label lb = (Label)Repeater.Items[i].FindControl("label1");
list.Add(lb.Text);
}
}}
多选的checkbox删除的SQL:sql="delete table where id in (" + vStr + "1)";
string vStr="";
改为StringBuilder vStr = new StringBuilder("");
把你选中的值得出来,做成string ids="‘1’,‘2’,‘3’"样式
放到
delete from tablename where id in(ids)不就完了吗得ID的值,遍历一下Repeater就行了个人见解,共同学习
if (result)
{
for (int i = 0; i < cblFunctionName.Items.Count; i++)
{
if (roleFunctionBL.SelectRoleFunctions(Convert.ToInt32(cblFunctionName.Items[i].Value), RoleId) > 0)//是否存在
{
if (!cblFunctionName.Items[i].Selected)
{
roleFunctionBL.DeleteRoleFunction(RoleId, Convert.ToInt32(cblFunctionName.Items[i].Value));//存在就删除
}
}
else
{
if (cblFunctionName.Items[i].Selected)
{
roleFunctionBL.InsertIdRoleFunction(RoleId, Convert.ToInt32(cblFunctionName.Items[i].Value));//没有就添加
}
}
}
ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('更新成功!');Exit();</script>");
}
else
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('更新失败!');Exit();</script>");
}
}