SUN
SUN

Reputation: 973

Make new dynamic CheckBoxList as per selected items of another checkboxlist in asp.net

I am making ecommerece website where I have certain filters (5 type of checkboxlists). If user apply any items from checkboxlist the selected items should be added to new checkboxlist. Basically I want to display what users have selected. I can able to do this with following code.

if (IsPostBack) {
    userSelections.Items.Clear();
    foreach (ListItem item in priceFilter.Items) {
        if (item.Selected) {
            userSelections.Items.Add(item);
        }
    }

    foreach (ListItem item in brandFilter.Items) {
        if (item.Selected) {
            userSelections.Items.Add(item);
        }
    }
}

With this code items get added to userselections CheckboxList but now i don't know if something from userselection gets uncheck it should be uncheck from it's main filter as well.Can any one help me to do this.

Upvotes: 0

Views: 705

Answers (3)

kan123
kan123

Reputation: 11

When item is unchecked from userSelections, you have to take the item value from list and loop through with priceFilter and brandFilter using userSelections CheckedChanged Event. When unchecked item value is match with the priceFilter and brandFilter item ,just uncheck from the priceFilter or brandFilter.

String unchecked_item = userSelections.item.value; (Note : unchecked item value)

    foreach (ListItem item in priceFilter.Items) {
            if (unchecked_item == item.value) {
                priceFilter.Item.selected = false;
            }
        }

foreach (ListItem item in brandFilter.Items) {
            if (unchecked_item == item.value) {
                brandFilter.Item.selected = false;
            }
        }

finally remove the unchecked item from the userSelections.

Upvotes: 1

Rakesh Yadav
Rakesh Yadav

Reputation: 113

You need to apply this on Apply Event for checkbox, not page load event. I think you applied in Page_Load isPostback.

Upvotes: 0

Tomato32
Tomato32

Reputation: 2245

Oh I think you can using JavaScript or CheckedChanged Event. When the user checks new checkboxlist, just remove it.

Upvotes: 0

Related Questions