标题:高手帮帮解释解释
只看楼主
jalonlovesja
Rank: 5Rank: 5
来 自:湖南邵阳
等 级:职业侠客
帖 子:292
专家分:371
注 册:2008-2-19
得分:0 
做批量删除好像不这么麻烦了,我做过全选删除了,里面包含可以全选的,单选的.
我想应该是你的要的结果吧!!!
<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();
    }

小弟,身高1.60米,农村户口,小学文化,家有农田1亩.
2008-04-23 13:10
馨馨
Rank: 1
等 级:新手上路
帖 子:136
专家分:0
注 册:2008-3-10
得分:0 
你这是什么语言的啊,我做的是C#,我的那个只是实现了时间段的删除,就是说这次添加的可以删,但是以前添加的就不能删了
2008-04-23 13:20
青格儿
Rank: 4
等 级:贵宾
威 望:11
帖 子:698
专家分:20
注 册:2007-7-31
得分:0 
楼主:这代码没有问题,我刚照着你这种办法做了一下,成功了,谢谢楼主的分享,下面是我做测试的代码:(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>");
            }
        }
全选没用你那个,下面的代码主要是参考你的改的,你看下,可以运行,你的报什么错?
2008-04-23 14:40
馨馨
Rank: 1
等 级:新手上路
帖 子:136
专家分:0
注 册:2008-3-10
得分:0 
你这个可以实现全选删除?
2008-04-23 15:45
青格儿
Rank: 4
等 级:贵宾
威 望:11
帖 子:698
专家分:20
注 册:2007-7-31
得分:0 
可以,在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实现全选!
2008-04-23 16:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-210573-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016019 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved