2

I've looked up many things and taken many suggestions, but for some reason can't seem to figure out why this code is not working to remove the selected items from my checkboxlist:

void btn_remove_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < UPCList.Items.Count; i++)
        {
            if (UPCList.Items[i].Selected == true)
            {
                UPCList.Items.RemoveAt(i);
            }

        }
        Response.Redirect("WebForm2.aspx?account=" + AcctNum.Text);
        Response.End();
    }
4

5 回答 5

0

解决方案:

List<ListItem> toBeRemoved = new List<ListItem>();
for(int i=0; i<UPCList.Items.Count; i++){
    if(UPCList.Items[i].Selected == true)
        toBeRemoved.Add(UPCList.Items[i]);
}

for(int i=0; i<toBeRemoved.Count; i++){
    UPCList.Items.Remove(toBeRemoved[i]);
}

希望能帮助到你

于 2013-06-14T17:43:07.887 回答
0

迈克尔:

您不能从正在迭代的列表中删除项目。您将要创建要删除的索引或项目的数组,然后在完成循环后,您将从数组中删除这些项目。

我认为@AnnArbor87 为您提供了执行此操作的代码。

HTH。

于 2013-06-21T21:29:47.870 回答
0

它最终成为 PICNIC 的一个大案例 :) 项目在数据库中,所以当页面刷新时,它只是再次检索我“删除”的项目。一旦我将它们从数据库中删除,一切都按预期工作。

于 2014-06-18T21:39:05.213 回答
-1

What is the point of removing items from a checkbox list then redirecting? You don't show the changes you are making

Webform2.aspx :

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

<asp:checkboxlist ID="UPCList" runat="server">
      <asp:ListItem>Item 1</asp:ListItem>
         <asp:ListItem>Item 2</asp:ListItem>
         <asp:ListItem>Item 3</asp:ListItem>
         <asp:ListItem>Item 4</asp:ListItem>
         <asp:ListItem>Item 5</asp:ListItem>
         <asp:ListItem>Item 6</asp:ListItem>
</asp:checkboxlist>
    </div>
        <asp:Button ID="btn_remove" runat="server" Text="Remove" OnClick="btn_remove_Click" />

    </form>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</body>
</html>

Webform2.aspx.cs :

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "Load";
        }

        public void btn_remove_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < UPCList.Items.Count; i++)
            {
                if (UPCList.Items[i].Selected == true)
                {
                    UPCList.Items.RemoveAt(i);
                }

            }
            Label1.Text = "Done";
        }
    }
}

Works as expected.

If I put in the

Response.Redirect("WebForm2.aspx?account=" + AcctNum.Text);

I get Item 1 ... 6 Remove

and a Label1 that says "Load"

also for clarity's sake i also like to do

  foreach (var toRemove in UPCList.Items.OfType<ListItem>().Where(f => f.Selected).ToList())
            {
                UPCList.Items.Remove(toRemove);
            }

but that's just personal opinion.

于 2013-06-14T17:45:36.067 回答
-1
for (int i = 0; i < UPCList.Items.Count; i++)
        {
            if (UPCList.SelectedIndex == i)
            {
                UPCList.Items.RemoveAt(i);
            }

        }

用这个。

于 2013-06-14T19:44:48.143 回答