mardi 3 juillet 2018

CheckboxList on deselect remove the item from Bulleted list

Hi I have this piece of code where I map the selected values of my check boxes to a Bulleted list so far it work ok when I select the value - it maps to bulleted list accordingly. However, I am struggling a bit when users deselects the value. I would like it to update the bulledted list accordingly. Any help?

Here is the code:

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
                    {

                    }}}}

The DOM elements:

  <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/>




Aucun commentaire:

Enregistrer un commentaire