注册 登录
编程论坛 ASP.NET技术论坛

高手帮帮解释解释

馨馨 发布于 2008-04-23 09:21, 1456 次点击
string usernos = String.Join(",", list.ToArray());//把它放到队列中
        try
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]))
            {
                conn.Open();
                string DeleteCmd = string.Format("delete from staff where userno in ({0})", usernos);
                using (SqlCommand MyCommand = new SqlCommand(DeleteCmd, conn))
                {
                    MyCommand.ExecuteNonQuery();
                }
                conn.Close();
            }
            GridView11.DataBind();
            Response.Write("<script language='javascript'>alert('成功删除!')</script>");
        }
        catch
        {
            Response.Write("<script>alert('删除失败!');history.back();</Script>");
        }
14 回复
#2
馨馨2008-04-23 09:22
是什么意思啊?尤其是 string DeleteCmd = string.Format("delete from staff where userno in ({0})", usernos);
#3
hebingbing2008-04-23 09:29
这段代码没有什么难得啊……
都是基本的代码……
string DeleteCmd = string.Format("delete from staff where userno in ({0})", usernos);
就相当于:
delete from staff where userno in usernos
那个{0}就代表usernos
#4
馨馨2008-04-23 09:44
我用这几条语句实现删除怎么总是说删除失败啊,总也删不了,也不报错,这是怎么回事啊,
string usernos = String.Join(",", list.ToArray());这个队列是怎么调用的啊,能不能给解释一下删除的思路啊,
#5
青格儿2008-04-23 10:23
楼主要做什么效果?还要用到队列?
#6
馨馨2008-04-23 11:18
做批量删除
#7
青格儿2008-04-23 11:59
批量删除?我也在做这,不过用的是循环,楼主要是做出来了,麻烦共享下,让我也学习学习,呵呵
#8
馨馨2008-04-23 12:13
恩  好的
#9
馨馨2008-04-23 12:16
在html中 :<script   language="javascript">   
  function   myCheck1()   
    {   
          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 CheckAllCus1()
 {
    var opt = document.getElementById("checkAccept");
    var tb = document.getElementById("<%=GridView1.ClientID%>");
    var arr = tb.getElementsByTagName("input");
    for(i=0;i<arr.length;i++){
        if(arr[i].type == "checkbox"){
            arr[i].checked = opt.checked;
        }
    }
}

在GRIDVIEW 中
  <asp:TemplateField>   
                        <HeaderStyle   Width="3%"></HeaderStyle>   
                        <HeaderTemplate>   
                        <asp:Label   id="Label4"   runat="server"> 全选 </asp:Label>
                        <INPUT   id="checkAccept"   type="checkbox"  onclick="CheckAllCus1()">   
                        </HeaderTemplate>   
                        <ItemTemplate>   
                        <asp:CheckBox   id="CB1"   runat="server"></asp:CheckBox>   
                        </ItemTemplate>   
                        </asp:TemplateField>

在.CS中  
   PAGE LOAD 中:
this.shanchu.Attributes.Add("onclick", "return(myCheck1());");

在删除按钮中:

 protected void shanchu_Click1(object sender, EventArgs e)
    {
        List<string> list1 = new List<string>();//定义一个list行的队列
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox cb1 = (CheckBox)GridView1.Rows[i].Cells[11].FindControl("CB1");//将GRIDVIEW中的每行的按钮的ID参数赋给 cb
            if (cb1.Checked == true)
            {
                list1.Add(GridView1.DataKeys[i].Value.ToString());//将选中的行记录到LIST中
            }
        }

        if (list1.Count <= 0)//没有选中的按钮
        {
            Response.Write("<script language='javascript'>alert('请选择要删除的项!')</script>");
            return;
        }


        string usernos = String.Join(",", list1.ToArray());
        try
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]))
            {
                conn.Open();
                string DeleteCmd = string.Format("delete from suppliers where supplyersid in ({0})", usernos);
                using (SqlCommand MyCommand = new SqlCommand(DeleteCmd, conn))
                {
                    MyCommand.ExecuteNonQuery();
                }
                conn.Close();
            }
            GridView1.DataBind();
            Response.Write("<script language='javascript'>alert('成功删除!')</script>");
        }
        catch
        {
            Response.Write("<script>alert('删除失败!');history.back();</Script>");
        }
    }
#10
馨馨2008-04-23 12:18
这是这个的思路,但是出了点问题,青格儿,你帮忙给改改,你运行一下
#11
jalonlovesja2008-04-23 13:10
做批量删除好像不这么麻烦了,我做过全选删除了,里面包含可以全选的,单选的.
我想应该是你的要的结果吧!!!
<asp:TemplateField HeaderText="全选">
                                <HeaderTemplate>
                                    <asp:CheckBox ID="CheckBox2" runat="server" Text="全选" onclick="ok(this)" />
                                </HeaderTemplate>
function ok(CheckBox)
    {
       var GridView = document.getElementById("GridView1");           
        for(var i=1;i<GridView.rows.length-1;i++)
        {
             GridView.rows[i].cells[0].getElementsByTagName("input")[0].checked=CheckBox.checked;
        }
   }
这是全选了,下面是删除的事件了.
protected void btnDelete_Click(object sender, EventArgs e)
    {

        SKK.BBL.FactoryReceiptCloth factory = new SKK.BBL.FactoryReceiptCloth();
        foreach (GridViewRow i in GridView1.Rows)
        {
            if ((i.FindControl("CheckBox1") as CheckBox).Checked == true)
            {
                int Fa_id = int.Parse(GridView1.DataKeys[i.RowIndex].Value.ToString());
                if (factory.FactoryReceiptClothDelete(Fa_id))
                {
                    ("删除成功!");
                }
                else
                {
                    ("删除失败!");
                }
            }
        }
        Bind();
    }
#12
馨馨2008-04-23 13:20
你这是什么语言的啊,我做的是C#,我的那个只是实现了时间段的删除,就是说这次添加的可以删,但是以前添加的就不能删了
#13
青格儿2008-04-23 14:40
楼主:这代码没有问题,我刚照着你这种办法做了一下,成功了,谢谢楼主的分享,下面是我做测试的代码:(GvInsert是GridView)
在删除按钮里写:
 List<string> list1 = new List<string>();
 bool flag;
 int i, chkcount = 0;
 for (i = 0; i < GvInsert.Rows.Count; i++)
        {
            flag = ((CheckBox)GvInsert.Rows[i].FindControl("CheckBox2")).Checked;
            if (flag)
            {
                chkcount++;
            }
        }
        if (chkcount == 0)
        {
            Response.Write("至少选一项");
            return;
        }
        for (i = 0; i < GvInsert.Rows.Count; i++)
        {
            flag = ((CheckBox)GvInsert.Rows[i].FindControl("CheckBox2")).Checked;
            if (flag)
            {
                list1.Add(GvInsert.DataKeys[i].Value.ToString());
            }
            string usernos = string.Join(",", list1.ToArray());
            try
            {
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DisplayDataConnectionString"].ConnectionString))
                {
                    conn.Open();

                    string DeleteCmd = string.Format("delete from PlanInsertManage where PlanInsertID in ({0})", usernos);
                    using (SqlCommand MyCommand = new SqlCommand(DeleteCmd, conn))
                    {
                        MyCommand.ExecuteNonQuery();
                    }
                    conn.Close();
Response.Write("<script language=javascript> window.location='PlanInsertList.aspx';</script>");
                }
            }
            catch
            {
                Response.Write("<script>alert('删除失败!');history.back();</Script>");
            }
        }
全选没用你那个,下面的代码主要是参考你的改的,你看下,可以运行,你的报什么错?
#14
馨馨2008-04-23 15:45
你这个可以实现全选删除?
#15
青格儿2008-04-23 16:53
可以,在GridView里加一列模板列,里面放上CheckBox
<asp:TemplateField>
                               <HeaderTemplate>
                                   <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox1_CheckedChanged" />
                               </HeaderTemplate>
                               <ItemTemplate>
                                   <asp:CheckBox ID="CheckBox2" runat="server" />
                               </ItemTemplate>
                           </asp:TemplateField>


protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < GvInsert.Rows.Count; i++)
        {
            CheckBox chkSelect = (CheckBox)GvInsert.Rows[i].FindControl("CheckBox2");

            chkSelect.Checked = !chkSelect.Checked;
        }
    }
这种全选最简单,但是有缺陷,你试试就知道了,最好用javascript实现全选!
1