用户要求对DATAGRID中的数据经过查询后进行批量删除,也就是满足条件查询出来的数据要全部删除. 我在SQL SERVER的存储过程中做了个 DELETE 产品信息 FROM where 产品id=@material_id.
DATAGRID中有产品ID, 供应商等等, 产品ID是隐藏的, 我想做个DELETE按钮, 在所有数据查询出来时,按DELETE把表中数据删除! 现在碰到的问题是: 如何取到所删除项的ID号,也就是我需要的@material_id.
DATAGRID中有产品ID, 供应商等等, 产品ID是隐藏的, 我想做个DELETE按钮, 在所有数据查询出来时,按DELETE把表中数据删除! 现在碰到的问题是: 如何取到所删除项的ID号,也就是我需要的@material_id.
{
Response.Redirect("DelMaterial.aspx?id="+e.Item.Cells[0].Text);
}
这个是我做的单个删除, 这里ID号是当前行, 如果要多个删除的话,是不是要做个循环? 要是改为 用CHECKBOX的方式,是不是比较可行点?
1。如果你dg没有checkbox,也有分页。那么你批量删除必须使用数据源,从数据源获取ID,传去sql,批量删除。
2。如果有checkbox,可以先循环取得选中的ID,再批量删除。你先决定工作方式,再考虑方法。
private void btdel_Click(object sender, System.EventArgs e)
{
for(int i=0;i<dgrdMaterialInfo.Items.Count;i++)
{
CheckBox mychk = (CheckBox)dgrdMaterialInfo.Items[i].FindControl("chkExport");
if(mychk.Checked == true)
{
Response.Redirect("DelMaterial.aspx?id="+dgrdMaterialInfo.Items[i].Cells[i].Text);
} }
}这是连接到下一页的确认页(DelMateial.aspx)的代码:
private void btnOk_Click(object sender, System.EventArgs e)
{
IQuotationMaterialDAL iqmd=new QuotationMaterialDAL(); iqmd.Delete(int.Parse(Request["id"].ToString()),0); Response.Redirect("QuotationMaterial.aspx");
}
这是QuotationMaterialDAl 中DELETE的代码
public void Delete(int id,int target)
{
SqlDbProxy sdp=new SqlDbProxy("sp_add_del_material");
sdp.AddParameter("@action","1");
sdp.AddParameter("@material_id",id);
sdp.AddParameter("@target",target); sdp.ExecuteNonQuery();
sdp.Close();
}
这样只能一条一条删除,你可以改成
DELETE 产品信息 FROM where 产品id in @Condition当你查询出记录时,把查询条件记录一下(或者通过循环把ID取出拼串),然后传入进行删除操作,这样效率会高很多啊!
if(!IsPostBack)
{
this.Button2.Attributes.Add("onclick","return(myCheck());");
} 删除按钮中:
SqlConnection MyConnection = new SqlConnection("server=(local);database=bysj;Trusted_Connection=yes");
string DeleteCmd = "DELETE FROM ygwage WHERE ygid = @ygid AND wagedate BETWEEN @LowerDate AND @UpperDate";
//And dixin=@dixin";
SqlCommand MyCommand = new SqlCommand(DeleteCmd, MyConnection);
MyCommand.Parameters.Add("@ygid", SqlDbType.Int);
MyCommand.Parameters.Add(new SqlParameter("@LowerDate", SqlDbType.DateTime));
MyCommand.Parameters["@LowerDate"].Value = Convert.ToDateTime(TextBox2.Text);
MyCommand.Parameters.Add(new SqlParameter("@UpperDate", SqlDbType.DateTime));
MyCommand.Parameters["@UpperDate"].Value = Convert.ToDateTime(TextBox3.Text);
bool IsChecked = false;
// 此回圈判断哪些核取方块已被勾选,并将相对应的资料记录删除。
foreach (DataGridItem CheckBoxItem in DataGrid1.Items)
{
IsChecked = ((CheckBox)CheckBoxItem.FindControl("CB")).Checked;
// if(IsChecked==false)
// {
// Page.RegisterStartupScript("Alert","<script language=javascript>alert('请选择要删除的项!');</script>");
// }
if (IsChecked)
{
// 取得使用者欲删除之资料记录的主索引键(亦即员工编号)
// 然後将它指派给 DELETE 命令叙述中的参数。
MyCommand.Parameters["@ygid"].Value =DataGrid1.DataKeys[CheckBoxItem.ItemIndex];
//MyCommand.Parameters["@dixin"].Value =DataGrid1.DataKeys[CheckBoxItem.ItemIndex];
MyCommand.Connection.Open();
try
{
// 呼叫 ExecuteNonQuery() 方法以便针对资料来源执行 DELETE 命令
MyCommand.ExecuteNonQuery();
Message1.Text = "成功删除";
Message1.Visible=true;
//Page.RegisterStartupScript("Alert","<script language=javascript>alert('成功删除!');</script>");
}
catch
//(SqlException)
{
Message.Text = "错误: 无法删除资料纪录";
//Response.Write("<script>alert('删除失败!');history.back();</Script>");
}
MyCommand.Connection.Close();
}
}
// 重新系结至资料来源
BindGridToSource();
}
前台JS:<script language="javascript">
function myCheck()
{
var mycount = 0 ;
var mm = document.getElementsByTagName("input").length ;
for(var i=0;i<mm;i++)
{
var dd = document.getElementsByTagName("input").item(i);
if(dd.type == "checkbox")
{
if(dd.checked == true)
{
mycount += 1;
}
}
} if(mycount == 0)
{
alert("您还未选择,请选择!");
return(false);
}
else
{
return(confirm("本次操作将删除该时间段的所有记录,注意!是该时间段!您真的要删除它吗?"));
}
}
</script>
全选:
<script language="javascript">
<!--
function CheckAllCus()
{
for (var k=0;k<document.Form1.elements.length;k++)
{
var e = document.Form1.elements[k];
if (e.id!= 'checkAccept')
e.checked = document.Form1.checkAccept.checked;
}
}
//-->
</script>
<script language="JavaScript">全选调用:
<asp:TemplateColumn>
<HeaderStyle Width="3%"></HeaderStyle>
<HeaderTemplate>
<INPUT id="checkAccept" type="checkbox" onclick="CheckAllCus()">
<asp:Label id="Label4" runat="server">全选</asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="CB" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
希望对你有帮助:)
for(int i=0;i<dgrdMaterialInfo.Items.Count;i++)
{
CheckBox mychk = (CheckBox)dgrdMaterialInfo.Items[i].FindControl("chkExport");
if(mychk.Checked == true)
{
Response.Redirect("DelAllMaterial.aspx?id="+dgrdMaterialInfo.Items[i].Cells[0].Text);
}
你选几个就删几个啊,全选就删除全部的咯,
foreach(BookCourse bo in bookcourse)
{
LabCourseBook.Text= "("+ bo.BookCode+")"+bo.BookName;
CourseBookCount+=LabCourseBook.Text + " , ";
}
if(bookcourse.Count==0)
{
LabCourseBook.Text=" ";
}
else
{
LabCourseBook.Text= CourseBookCount.Substring(0,CourseBookCount.Length-2) ;
}