?
Solved

checkboxlist

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

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

771 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