Display all selected items in a CheckBox List as a single text
Actually the situation is there are 3 or more items in checkbox list.For example the items are as displayed below.
1. 1 room
2. 2 room
3. 3 room & so on
Now if we select 2&3 items we get 2 room&3 room. but the output should be 2 & 3 room. I suggest a way for this is as follows:
//First add System.Collections NameSpace inorder to get ArrayList
ArrayList arr = new ArrayList();
//Specify the word which u want to trim.
string trim = "room";
//Loop to rotate through all the items in checkboxlist.
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
//To know which item is selected.
if(CheckBoxList1.Items[i].Selected)
{
//Adding the selected items to arraylist by trimming the 'trim' string
arr.Add(CheckBoxList1.Items[i].Text.Trim(trim.ToCharArray()));
}
}
//A new Array is created.
string[] str=new string[arr.Count];
//Items in arraylist is copied into new array inorder to use join method
arr.CopyTo(str);
//Finally to get Desired Output we are adding '&' between items in array & 'trim' string.
string final= string.Join("&",str)+trim;
(CheckBoxList1.Items[i].Selected) always come False whether it is checked or not.