Solved

checkboxlist

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now