Welcome to Dream.In.Code
Become an Expert!

Join 150,045 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,626 people online right now. Registration is fast and FREE... Join Now!




CheckBoxList Selection

 
Reply to this topicStart new topic

CheckBoxList Selection

Footsie
10 Feb, 2008 - 05:35 AM
Post #1

D.I.C Regular
Group Icon

Joined: 20 Sep, 2007
Posts: 308



Thanked: 4 times
Dream Kudos: 50
My Contributions
I've set up a CheckBoxList to take a selection from a user, and bound it to my database (MySQL) so that it does a SELECT DISTINCT manufacturer FROM basevehicle; and populates the check box with the result.

Everything works fine but when it comes to checking a box, the CheckBoxList allows you to select more than one box.

I've worked around this problem with the following code:
CODE

   protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //get the manufacturer that the user selected
        string manufSelect = CheckBoxList1.SelectedValue.ToString();
        Label1.Text = manufSelect;

        //Disable the check box so only one box can be selected
            CheckBoxList1.Enabled = false;
    }


I have enabled AutoPostBack in the designer, so that the check box gets disabled after a selection is made.

What I would like to do though, is not to disable the check box, but still only take one check.

How can I do this?

This post has been edited by Footsie: 10 Feb, 2008 - 05:37 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: CheckBoxList Selection
10 Feb, 2008 - 07:17 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Footsie,

The following little snippet will allow only a single selection in a CheckBoxList. It will cause the CheckBoxList to work like RadioButtons, where only a single item can be selected, I hope this is what you were looking for smile.gif


CODE

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (checkedListBox1.CheckedItems.Count > 0)
    {
        foreach (int i in checkedListBox1.CheckedIndices)
        {
            checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
        }
    }
}


Happy Coding! smile.gif
User is offlineProfile CardPM
+Quote Post

Footsie
RE: CheckBoxList Selection
10 Feb, 2008 - 08:06 AM
Post #3

D.I.C Regular
Group Icon

Joined: 20 Sep, 2007
Posts: 308



Thanked: 4 times
Dream Kudos: 50
My Contributions
QUOTE(PsychoCoder @ 10 Feb, 2008 - 05:17 PM) *

Footsie,

The following little snippet will allow only a single selection in a CheckBoxList. It will cause the CheckBoxList to work like RadioButtons,


I didn't even think about RadioButtons! I tried setting it up as a RadioButtonList instead and that works perfectly.

I then tried adding your code snippet to my original CheckBoxList to try and see if that route worked but it gave me the following error:
'System.Web.UI.WebControls.CheckBoxList' does not contain a definition for 'CheckedItems'


Why do you think this is?
EDIT: also the intellisense didn't seem to pick up 'CheckedItems' and 'CheckedIndices' 'CheckState' as I typed them in...

This post has been edited by Footsie: 10 Feb, 2008 - 08:09 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: CheckBoxList Selection
10 Feb, 2008 - 08:29 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You are right, I apologize I provided a snippet for a Windows Forms control. I will find the snippet for a web form control I just have to look through my snippet collection here.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: CheckBoxList Selection
10 Feb, 2008 - 02:01 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
QUOTE(Footsie @ 10 Feb, 2008 - 08:06 AM) *

I didn't even think about RadioButtons! I tried setting it up as a RadioButtonList instead and that works perfectly.

I then tried adding your code snippet to my original CheckBoxList to try and see if that route worked but it gave me the following error:
'System.Web.UI.WebControls.CheckBoxList' does not contain a definition for 'CheckedItems'


Why do you think this is?
EDIT: also the intellisense didn't seem to pick up 'CheckedItems' and 'CheckedIndices' 'CheckState' as I typed them in...

That is because WebControls do not inherit from the same controls as Windows.Form controls. The have there own methods and properties.

http://msdn2.microsoft.com/en-us/library/s...st_members.aspx

Stick with the RadioButtonList, they are the best solution for this situation.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:01PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month