Solved

checkboxlist

Posted on 2011-09-05
4
497 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

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio hangs on running project 6 62
Retreiving SOAP FAULT messages using classical ASP 14 31
window close link 7 35
ComboBox to String Not Working 5 20
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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