当前位置:编程学习 > C#/ASP.NET >>

ASP.NET批量删除的问题 ····求解

批量删除的那个按钮不起作用,请帮忙看一下是怎么回事?
aspx代码

 <script type="text/javascript">
        function CheckAll(Obj) {
             var AllObj = document.all;
             if (Obj.checked)//全选
             {
                 for (var i = 0; i < AllObj.length; i++) {
                     if (AllObj[i].type == "checkbox") {
                         AllObj[i].checked = true;
                     }
                }
             }
            else//反选
            {
                 for (var i = 0; i < AllObj.length; i++) {
                     if (AllObj[i].type == "checkbox") {
                         AllObj[i].checked = false;
                    }
                }
             }
        }
     
     </script>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" Width="355px" 
            onitemcommand="DataList1_ItemCommand" DataKeyField="id"> 
            <HeaderTemplate>
                <asp:CheckBox ID="CheckBox2" runat="server" onclick="return CheckAll(this)" />
                <asp:Label ID="Label3" Width="70px" runat="server" Text="ID"></asp:Label>
                <asp:Label ID="Label4" Width="170px" runat="server" Text="imageUrl"></asp:Label>
                <asp:Label ID="Label5"  runat="server" Text="handle"></asp:Label>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" />
                <asp:Label ID="Label1" Width="70px" runat="server" Text='<%# Eval( "id") %>'></asp:Label>
                <asp:Label ID="Label2" Width="170px" runat="server" Text='<%# Eval("imageUrl") %>'></asp:Label>
                <asp:Button ID="Button1" runat="server" CommandName="singleDelete" Text="delete" />
            </ItemTemplate>
             <FooterTemplate>
                <asp:Button ID="Button2" runat="server"  CommandName="mutlDelete" Text="deleteAll" />
            </FooterTemplate>
        </asp:DataList>
    </div>
    </form>
</body>


aspx.cs代码

引用
public partial class _234DeleteData : System.Web.UI.Page
    {
        ShowImageBll showImageBll = new BLL.ShowImageBll();
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataList();
            }
        }

        private void BindDataList()
        {
            DataSet ds = showImageBll.GetList();
            DataList1.DataSource = ds;
            DataList1.DataBind();
        }
        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
          switch (e.CommandName)
            {
                case "singleDelete":
                    int id = int.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());
                    if (showImageBll.GetList()!=null)
                    {
                        showImageBll.DeleteSingleList(id);
                        Response.Write("<script>alert('删除成功!')</script>");
                        BindDataList();//重新绑定数据库
                    }
                    else 
                    {
                        showImageBll.DeleteSingleList(id);
                        Response.Write("<script>alert('删除失败!')</script>");
                    }
                    break;
                case "mutlDelete":

                    DataListItemCollection dlic = DataList1.Items;//创建一个DataList列表项集合对象
                    for (int i = 0; i < dlic.Count; i++)
                    {
                        if (dlic[i].ItemType == ListItemType.AlternatingItem || dlic[i].ItemType == ListItemType.Item)
                        {
                              CheckBox cb=(CheckBox)dlic[i].FindControl("CheckBox1");
                              if (cb.Checked)
                              {
                                  int id1 = int.Parse(DataList1.DataKeys[dlic[i].ItemIndex].ToString());
                                  showImageBll.DeleteSingleList(id1);      
                              }
                        }
                    }
                    BindDataList();//重新绑定数据库
                    break;
                    
          
          }

        }
    }
--------------------编程问答-------------------- 单步调试看看是值没有取到(全选、反选),还是删除不起作用(DeleteSingleList)。 --------------------编程问答--------------------
引用 楼主 abc8023 的回复:
批量删除的那个按钮不起作用,请帮忙看一下是怎么回事?
aspx代码

 <script type="text/javascript">
        function CheckAll(Obj) {
             var AllObj = document.all;
             if (Obj.checked)//全选
             {
                 for (var i = 0; i < AllObj.length; i++) {
                     if (AllObj[i].type == "checkbox") {
                         AllObj[i].checked = true;
                     }
                }
             }
            else//反选
            {
                 for (var i = 0; i < AllObj.length; i++) {
                     if (AllObj[i].type == "checkbox") {
                         AllObj[i].checked = false;
                    }
                }
             }
        }
     
     </script>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" Width="355px" 
            onitemcommand="DataList1_ItemCommand" DataKeyField="id"> 
            <HeaderTemplate>
                <asp:CheckBox ID="CheckBox2" runat="server" onclick="return CheckAll(this)" />
                <asp:Label ID="Label3" Width="70px" runat="server" Text="ID"></asp:Label>
                <asp:Label ID="Label4" Width="170px" runat="server" Text="imageUrl"></asp:Label>
                <asp:Label ID="Label5"  runat="server" Text="handle"></asp:Label>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" />
                <asp:Label ID="Label1" Width="70px" runat="server" Text='<%# Eval( "id") %>'></asp:Label>
                <asp:Label ID="Label2" Width="170px" runat="server" Text='<%# Eval("imageUrl") %>'></asp:Label>
                <asp:Button ID="Button1" runat="server" CommandName="singleDelete" Text="delete" />
            </ItemTemplate>
             <FooterTemplate>
                <asp:Button ID="Button2" runat="server"  CommandName="mutlDelete" Text="deleteAll" />
            </FooterTemplate>
        </asp:DataList>
    </div>
    </form>
</body>


aspx.cs代码

引用
public partial class _234DeleteData : System.Web.UI.Page
    {
        ShowImageBll showImageBll = new BLL.ShowImageBll();
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataList();
            }
        }

        private void BindDataList()
        {
            DataSet ds = showImageBll.GetList();
            DataList1.DataSource = ds;
            DataList1.DataBind();
        }
        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
          switch (e.CommandName)
            {
                case "singleDelete":
                    int id = int.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());
                    if (showImageBll.GetList()!=null)
                    {
                        showImageBll.DeleteSingleList(id);
                        Response.Write("<script>alert('删除成功!')</script>");
                        BindDataList();//重新绑定数据库
                    }
                    else 
                    {
                        showImageBll.DeleteSingleList(id);
                        Response.Write("<script>alert('删除失败!')</script>");
                    }
                    break;
                case "mutlDelete":

                    DataListItemCollection dlic = DataList1.Items;//创建一个DataList列表项集合对象
                    for (int i = 0; i < dlic.Count; i++)
                    {
                        if (dlic[i].ItemType == ListItemType.AlternatingItem || dlic[i].ItemType == ListItemType.Item)
                        {
                              CheckBox cb=(CheckBox)dlic[i].FindControl("CheckBox1");
                              if (cb.Checked)
                              {
                                  int id1 = int.Parse(DataList1.DataKeys[dlic[i].ItemIndex].ToString());
                                  showImageBll.DeleteSingleList(id1);      
                              }
                        }
                    }
                    BindDataList();//重新绑定数据库
                    break;
                    
          
          }

        }
    }


不起作用,你放到ItemTemplate 里面测试下,另外,你批量删除逻辑有问题!

里面应该是循环的,你这样是批量不了的! --------------------编程问答-------------------- 不知道楼主点全选的时候页面有没有全部选中?? --------------------编程问答--------------------
引用 3 楼 itmaxin 的回复:
不知道楼主点全选的时候页面有没有全部选中??
有啊 --------------------编程问答--------------------
引用 1 楼 guwei4037 的回复:
单步调试看看是值没有取到(全选、反选),还是删除不起作用(DeleteSingleList)。

单步调试执行到if这句就直接跳出来了,不执行if里面的

if (dlic[i].ItemType == ListItemType.AlternatingItem || dlic[i].ItemType == ListItemType.Item)
                        {
                              CheckBox cb=(CheckBox)dlic[i].FindControl("CheckBox1");
                              if (cb.Checked)
                              {
                                  int id1 = int.Parse(DataList1.DataKeys[dlic[i].ItemIndex].ToString());
                                  showImageBll.DeleteSingleList(id1);      
                              }
                        }
--------------------编程问答-------------------- dlic[i].ItemType,type不对,你看看你的是什么type。 --------------------编程问答--------------------
引用 4 楼 abc8023 的回复:
Quote: 引用 3 楼 itmaxin 的回复:

不知道楼主点全选的时候页面有没有全部选中??
有啊
楼主删除单个可以成功吗? --------------------编程问答--------------------

if (dlic[i].ItemType == ListItemType.AlternatingItem || dlic[i].ItemType == ListItemType.Item)
                        {
                              CheckBox cb=(CheckBox)dlic[i].FindControl("CheckBox1");
                              if (cb.Checked)
                              {
                                  int id1 = int.Parse(DataList1.DataKeys[dlic[i].ItemIndex].ToString());
                                  showImageBll.DeleteSingleList(id1);      
                              }
                        }
感觉这个外层if可以不要。 --------------------编程问答--------------------
引用 6 楼 guwei4037 的回复:
dlic[i].ItemType,type不对,你看看你的是什么type。

请教应该改成什么  --------------------编程问答--------------------
引用 2 楼 hou306010849 的回复:
Quote: 引用 楼主 abc8023 的回复:

批量删除的那个按钮不起作用,请帮忙看一下是怎么回事?
aspx代码

 <script type="text/javascript">
        function CheckAll(Obj) {
             var AllObj = document.all;
             if (Obj.checked)//全选
             {
                 for (var i = 0; i < AllObj.length; i++) {
                     if (AllObj[i].type == "checkbox") {
                         AllObj[i].checked = true;
                     }
                }
             }
            else//反选
            {
                 for (var i = 0; i < AllObj.length; i++) {
                     if (AllObj[i].type == "checkbox") {
                         AllObj[i].checked = false;
                    }
                }
             }
        }
     
     </script>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" Width="355px" 
            onitemcommand="DataList1_ItemCommand" DataKeyField="id"> 
            <HeaderTemplate>
                <asp:CheckBox ID="CheckBox2" runat="server" onclick="return CheckAll(this)" />
                <asp:Label ID="Label3" Width="70px" runat="server" Text="ID"></asp:Label>
                <asp:Label ID="Label4" Width="170px" runat="server" Text="imageUrl"></asp:Label>
                <asp:Label ID="Label5"  runat="server" Text="handle"></asp:Label>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" />
                <asp:Label ID="Label1" Width="70px" runat="server" Text='<%# Eval( "id") %>'></asp:Label>
                <asp:Label ID="Label2" Width="170px" runat="server" Text='<%# Eval("imageUrl") %>'></asp:Label>
                <asp:Button ID="Button1" runat="server" CommandName="singleDelete" Text="delete" />
            </ItemTemplate>
             <FooterTemplate>
                <asp:Button ID="Button2" runat="server"  CommandName="mutlDelete" Text="deleteAll" />
            </FooterTemplate>
        </asp:DataList>
    </div>
    </form>
</body>


aspx.cs代码

引用
public partial class _234DeleteData : System.Web.UI.Page
    {
        ShowImageBll showImageBll = new BLL.ShowImageBll();
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataList();
            }
        }

        private void BindDataList()
        {
            DataSet ds = showImageBll.GetList();
            DataList1.DataSource = ds;
            DataList1.DataBind();
        }
        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
          switch (e.CommandName)
            {
                case "singleDelete":
                    int id = int.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());
                    if (showImageBll.GetList()!=null)
                    {
                        showImageBll.DeleteSingleList(id);
                        Response.Write("<script>alert('删除成功!')</script>");
                        BindDataList();//重新绑定数据库
                    }
                    else 
                    {
                        showImageBll.DeleteSingleList(id);
                        Response.Write("<script>alert('删除失败!')</script>");
                    }
                    break;
                case "mutlDelete":

                    DataListItemCollection dlic = DataList1.Items;//创建一个DataList列表项集合对象
                    for (int i = 0; i < dlic.Count; i++)
                    {
                        if (dlic[i].ItemType == ListItemType.AlternatingItem || dlic[i].ItemType == ListItemType.Item)
                        {
                              CheckBox cb=(CheckBox)dlic[i].FindControl("CheckBox1");
                              if (cb.Checked)
                              {
                                  int id1 = int.Parse(DataList1.DataKeys[dlic[i].ItemIndex].ToString());
                                  showImageBll.DeleteSingleList(id1);      
                              }
                        }
                    }
                    BindDataList();//重新绑定数据库
                    break;
                    
          
          }

        }
    }


不起作用,你放到ItemTemplate 里面测试下,另外,你批量删除逻辑有问题!

里面应该是循环的,你这样是批量不了的!

逻辑没有问题吧 --------------------编程问答-------------------- 楼主先看看showImageBll.DeleteSingleList(id);这个能起作用不 --------------------编程问答--------------------
引用 11 楼 dotnetstudio 的回复:
楼主先看看showImageBll.DeleteSingleList(id);这个能起作用不

可以 --------------------编程问答-------------------- 楼主的 批量删除按钮写在了页脚上,就不是ListItemType其中的类型了而是ListItemType.Footer。自然不能进入if后面的代码了。 --------------------编程问答--------------------
引用 13 楼 q274925416 的回复:
楼主的 批量删除按钮写在了页脚上,就不是ListItemType其中的类型了而是ListItemType.Footer。自然不能进入if后面的代码了。

那这句应该怎么样改 求指教 --------------------编程问答-------------------- 看这么多代码头就晕了。。。给楼主建议
Firefox---Firebug自己调试去。。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,