views:

494

answers:

4

Hey guys, I have a checkboxlist wich contains a list of services loaded from a database table... Each of these services can only be executed alone or with some other specific services. Like, If I select "Transfer Property" i cannot select "Register" at the same time...

I have a table which contains the relationships between Services and what Services can be selected together with each service (did I explained it right?).

What I need is to Click a service and then Disable/Disallow Checking of all the services that don't are related to that service, and Re-enable that items when i Uncheck the parent item...

Is there a good way to do this? I mean, is there any way to do this? because i couldn't... thank you in advance..

A: 
    void ControlCheckBoxList(int selected, bool val)
    {
        switch (selected)
        {
            case 1:
            case 2:
            case 3:
                checkedListBox1.SetItemChecked(5, !val);
                break;
            case 6:
                checkedListBox1.SetItemChecked(1, true);
                break;
            default:
                checkedListBox1.ClearSelected();
                break;
        }
    }

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        ControlCheckBoxList(e.Index, e.NewValue == CheckState.Checked ? true : false);
    }
Shimmy
A: 

1) Force your checkbox to auto-postback and then on the post back check what value was checked/unchecked and then enable/disable the other checkboxes as required.

2) Try to do something similar with AJAX.

mundeep
A: 

Well, javascript maybe best for doing this client side, otherwise you can try something like this in the code.

        List<Service> services = new List<Service>(); //get your services

        foreach (ListItem li in lstRoles.Items)
        {
            Predicate<Service> serviceIsAllowed = delegate (Service s) { return /*some expression using s and li.Value (or use a lambda expr) */; }

            li.Selected = services.Find(serviceIsAllowed) != null;
        }
CRice
A: 

First of all, your CheckBoxList will need to have AutoPostBack set to true.

I believe the key to what youre looking for is

   CheckBoxList1.Items.FindByText(service).Enabled = false;

or this works too

   CheckBoxList1.Items.FindByText(service).Attributes.Add("disabled", "disabled");

in context, it could look something like this:

    <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" 
        onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">        
    </asp:CheckBoxList>

    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        //first reset all to enabled
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            CheckBoxList1.Items[i].Attributes.Remove("disabled", "disabled");
        }

        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {

            if (CheckBoxList1.Items[i].Selected)
            {
                //get list of items to disable
                string selectedService = CheckBoxList1.Items[i].Text;
                List<string> servicesToDisable = getIncompatibleFor(selectedService);//this function is up to u
                foreach (string service in servicesToDisable)
                {
                    CheckBoxList1.Items.FindByText(service).Attributes.Add("disabled", "disabled");                                                
                }                   
            }
        }
    }
Tion