Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
To Checked CheckBoxList item I am using following code

C#
string pyDetails = objBiltyDetails.BltPayDetails.ToString();
            string[] split = pyDetails.Split(',');
            foreach (string item in split)
            {

                //if (item != "")
                //{

                    CheckBxPayDetails.SelectedValue = item;

                //}
            }


There are two values in item but only one check box is Checked. Please help
Posted
Updated 8-Jun-11 21:30pm
v3

Yes obviously the last item in the enumeration will be set as SelectedValue because that's what you have coded.

If you are trying to select all checkboxes under the CheckBoxList, then try the below line of code in your foreach loop:

CheckBxPayDetails.Items.Add(new ListItem() { Text = item, Selected = true });
 
Share this answer
 
v3
Comments
anjali2 9-Jun-11 3:21am    
It is CheckBoxList
RakeshMeena 9-Jun-11 3:32am    
Please see my updated answer.
OriginalGriff 9-Jun-11 3:36am    
Reason for my vote of one: If you look at the specification for CheckedListBox, you will find that multiple selections are not supported, so even your revised answer (which adds new items every time it is used, rather than selects existing ones and is thus a bit odd) cannot work! Please, check your answers before you post them! See MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.selectionmode.aspx
RakeshMeena 9-Jun-11 3:40am    
Sir! It's not CheckedListBox as you said. As the Anjutlya said this is a CheckBoxList.
Further the control you are mentioning is part of WinForms not asp.net (the question is tagged to ASP.net).
Don't be in hurry to downvote somebody just because they have different views.
anjali2 9-Jun-11 3:48am    
Yes its true, Rakesh sir I am using CheckBoxList in asp.net But I am not trying to add item I am trying to Checked items.
Please ignore this answer - I assumed CheckedBoxList rather than CheckBoxList.

If you look at the specification for CheckedBoxList you will see that MultiSelection is not supported, so it isn't going to work anyway. Even if you did use code that might select two items, such as
CheckBxPayDetails.SelectedItems.Add(item);
(Which as I said, won't work because CheckedBoxList doesn't support multiple selections.

Are you sure you want to use Select? Or are you trying to use Checked?

"I am trying to checked"

Try:
string pyDetails = objBiltyDetails.BltPayDetails.ToString();
string[] split = pyDetails.Split(',');
foreach (string item in split)
    {
    int index = CheckBxPayDetails.FindString(item);
    if (index >= 0)
        {
        CheckBxPayDetails.SetItemChecked(index, true);
        }
    }
You may want to loop through them all first, setting the check status to false - I don't know what you app does!
 
Share this answer
 
v3
Comments
anjali2 9-Jun-11 3:21am    
I am trying to checked
OriginalGriff 9-Jun-11 3:32am    
Answer updated
anjali2 9-Jun-11 3:36am    
Thanks for your reply
but there will be a error like -
Does not contain Definition for "FindString" and "SetItemChecked"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900