[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

checkboxlist

Posted on 2011-09-05
4
Medium Priority
?
509 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
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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

868 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