Solved

checkboxlist

Posted on 2011-09-05
4
498 Views
Last Modified: 2012-05-12
Hey guys i have a question.

i have a asp checkboxlist with the following items
All
Buffet
Chinese
Indian
Italian
Portuguese
Seafood
Other

i want if the user select all that all be selected and if the user select Buffet and Indian than that only two items become selected.

if you know what i mean?

please help?

i am coding in c#
0
Comment
Question by:JCWEBHOST
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 15

Expert Comment

by:gplana
ID: 36483242
I don't know c# but you just should add the logic for it: program the event onClicked (or something like this) of every checkbox (except all) and do a function which checks if all (buffet, ... other) are checked then check All checkbox.

This is the idea. Hope it helps.
0
 
LVL 7

Expert Comment

by:kovilpattiBalu
ID: 36483974
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 36484520
Hi,

I'm not sure if you want AutoPostback on your control but if that's ok then this is a solution:

in the .aspx:

    <asp:CheckBoxList ID="CheckBoxList1" 
        onselectedindexchanged="CheckBoxList1_SelectedIndexChanged" runat="server" 
        AutoPostBack="True">
        <asp:ListItem Text="All" />
        <asp:ListItem Text="Buffet" />
        <asp:ListItem Text="Chinese" />
        <asp:ListItem Text="Indian" />
        <asp:ListItem Text="Italian" />
        <asp:ListItem Text="Portuguese" />
        <asp:ListItem Text="Seafood" />
        <asp:ListItem Text="Other" />
    </asp:CheckBoxList>

Open in new window


In the code-behind (.aspx.cs), within your page class:

        private bool cbl_AllSelected = false; // remember if 'All' was selected previously

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                cbl_AllSelected = bool.Parse(ViewState["cbl_AllSelected"].ToString());
            }
            ViewState["cbl_AllSelected"] = cbl_AllSelected;
        }

        protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!cbl_AllSelected && this.CheckBoxList1.Items[0].Selected) // select all
            {
                cbl_AllSelected = true;
                for (int i = 1; i < this.CheckBoxList1.Items.Count; i++)
                {
                    this.CheckBoxList1.Items[i].Selected = true;
                }
            }
            else if (cbl_AllSelected && !this.CheckBoxList1.Items[0].Selected) // deselect all
            {
                cbl_AllSelected = false;
                this.CheckBoxList1.SelectedIndex = -1;
            }
            else if (cbl_AllSelected) // another item was clicked -> clear the 'All' checkbox
            {
                cbl_AllSelected = false;
                this.CheckBoxList1.Items[0].Selected = false;
            }
            ViewState["cbl_AllSelected"] = cbl_AllSelected;
        }

Open in new window


If you already have code in your Page_Load (not unlikely ;) then integrate the new code, maybe just put the new 4 lines in it right before the end "}".

So what this does is: remember (in the Viewstate) whether the All option was selected (default it is not). When the next time it gets selected, select all. When next time it gets deselected, deselect all. When it stays the same, another option must have been clicked so if All was selected, then just unselect it, otherwise do nothing.
0
 
LVL 16

Expert Comment

by:disrupt
ID: 36485254
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question