取消选中CheckboxList后,从项目符号列表中删除该项目

时间:2018-07-03 13:26:48

标签: c# asp.net list for-loop checkbox

嗨,我有这段代码,我在其中将复选框的选定值映射到项目符号列表,到目前为止,当我选择该值时,它可以正常工作-它相应地映射到项目符号列表。但是,当用户取消选择该值时,我有点挣扎。我希望它相应地更新项目符号列表。有帮助吗?

代码如下:

 public List<string> elements = new List<string>();


protected void checkList_SelectedIndexChanged(object sender, EventArgs e)
        { foreach(ListItem item in checkList.Items)
            {
                if (item.Selected)
                {
                     elements.Add(item.Text);
                }
                else {//Should this else triger the deletion of the elements in the list? 
                }

            }
            foreach (object o in elements)
            {   int exists = 0;
                for (int i = 0; i < BullList.Items.Count; i++)
                {

                    if(BullList.Items[i].ToString() == o.ToString())
                    {
                        exists++;
                    }
                }
                if(exists == 0)
                {
                    BullList.Items.Add(o.ToString());
                }}}

        protected void checkList_DataBound(object sender, EventArgs e)
        {
            for (int i = 0; i < BullList.Items.Count; i++)
            {
                foreach (ListItem item in checkList.Items)
                {
                    if (BullList.Items[i].ToString() == item.ToString())
                    {
                        item.Selected = true;
                    }
                    else
                    {

                    }}}}

DOM元素:

  <asp:DropDownList AutoPostBack="true" ID="DropDownJobs" runat="server" Width="390px" Height="42px" Font-Names="Roboto" Font-Size="25px" DataSourceID="jobsDrop" DataTextField="MainJob" DataValueField="IDKey"></asp:DropDownList>       

           <asp:CheckBoxList OnDataBound="checkList_DataBound" AutoPostBack="true" ID="checkList" runat="server" OnSelectedIndexChanged="checkList_SelectedIndexChanged" DataSourceID="SortedListJobs" DataTextField="SubJobFamily" DataValueField="SubJobFamily" Width="406px">
            <asp:ListItem>
            </asp:ListItem>  
            </asp:CheckBoxList>

            <h6 class="List">These jobs have been selected:</h6>
            <asp:BulletedList runat="server" ID="BullList" CssClass="bullList"></asp:BulletedList/>

1 个答案:

答案 0 :(得分:1)

就像您要向BulletedList添加项目一样,创建list中的deselect item。然后将其从BulletedList中删除。像下面一样

 public List<string> elements = new List<string>();
 public List<string> deselect = new List<string>();


    protected void checkList_SelectedIndexChanged(object sender, EventArgs e)
    { foreach(ListItem item in checkList.Items)
        {
            if (item.Selected)
            {
                 elements.Add(item.Text);
            }
            else 
            {
             deselect.Add(item.Text);
            }

        }
        foreach (object o in elements)
        {   int exists = 0;
            for (int i = 0; i < BullList.Items.Count; i++)
            {

                if(BullList.Items[i].ToString() == o.ToString())
                {
                    exists++;
                }
            }
            if(exists == 0)
            {
                BullList.Items.Add(o.ToString());
            }
         }
      foreach (object o in deselect)
        {
            for (int i = 0; i < BullList.Items.Count; i++)
            {
                BullList.Items.Remove(o.ToString());
            }
        }
     }