We want to perform checkbox multi selection in radcombobox.
We achieve multi selection functionality but comma separated text is not accepted by telerik API
We tried using setText method but telerik combo box sets internally only last selected value of checkbox.
Below is sample code along with Javascript functions
public class RadComboCustomControl : RadComboBox
{
private string txtValue;
protected override void PerformDataBinding(IEnumerable dataSource)
{
if (dataSource != null)
{
foreach (object dataItem in dataSource)
{
if (DataTextField.Length > 0)
{
txtValue = DataBinder.GetPropertyValue(dataItem, DataTextField, null);
Items.Add(new RadCustomComboBoxItem(txtValue));
}
}
}
}
}
public class RadCustomComboBoxItem : RadComboBoxItem
{
public RadCustomComboBoxItem(string text)
: base(text)
{
}
public RadCustomComboBoxItem()
: base()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
CheckBox chkBox = new CheckBox();
chkBox.Text = this.Text;
chkBox.Attributes.Add("onclick", "CheckboxClick();");
this.Controls.Add(chkBox);
}
}
JAVA Script Code
function CheckboxClick()
{
collectSelectedItems();
}
function getItemCheckBox(item)
{
//Get the 'div' representing the current RadComboBox Item.
var itemDiv = item.get_element();
//Get the collection of all 'input' elements in the 'div' (which are contained in the Item).
var inputs = itemDiv.getElementsByTagName("input");
for (var inputIndex = 0; inputIndex < inputs.length; inputIndex++) {
var input = inputs[inputIndex];
//Check the type of the current 'input' element.
if (input.type == "checkbox") {
return input;
}
}
return null;
}
function collectSelectedItems()
{
var combo = $find("<%=radComboBox1.ClientID%>");
var items = combo.get_items();
var selectedItemsTexts = "";
var selectedItemsValues = "";
var itemsCount = items.get_count();
for (var itemIndex = 1; itemIndex < itemsCount; itemIndex++)
{
var item = items.getItem(itemIndex);
var checkbox = getItemCheckBox(item);
//Check whether the Item's CheckBox) is checked.
if(checkbox!=null)
{
if (checkbox.checked)
{
selectedItemsTexts += item.get_text() + ", ";
selectedItemsValues += item.get_value() + ", ";
}
}
}
selectedItemsTexts = selectedItemsTexts.substring(0, selectedItemsTexts.length - 2);
selectedItemsValues = selectedItemsValues.substring(0, selectedItemsValues.length - 2);
//Set the text of the RadComboBox with the texts of the selected Items, separated by ','.
combo.set_text(selectedItemsTexts);
}