I am creating and populating some comboboxes in code for a page using 2008 SP1.
Everything seems to work fine except that it will automatically set the selected item to the first item in the list (selectedindex = 0)
If I set the selectedindex to -1 it will revert to 0. Adding an empty item to the combobox will then show the empty text but it seems a bit clunky.
Is this behaviour of automatically selecting the first combobox item (and therefore not showing the emptymessage either) correct?
code extract below :-
RadComboBox cmb = new RadComboBox();
//Set the ID
cmb.ID = "cmb_" + row.MyID;
cmb.SelectedIndex = -1;
//Add the control to the listitem container
item.Controls.Add(cmb);
//Get the possible values
string[] values = Value.Split('|');
//Custom or fixed list?
cmb.AllowCustomText = Custom;
if (Custom)
cmb.EmptyMessage = "Select a value or enter your own";
else
cmb.EmptyMessage = "Select a value";
//Will show empty message if I add a blank item to the combo first
//RadComboBoxItem emptyItem = new RadComboBoxItem(String.Empty);
//cmb.Items.Add(emptyItem);
//emptyItem.Selected = true;
foreach (string value in values)
{
RadComboBoxItem cmbItem = new RadComboBoxItem(value);
if (cmbItem.Text == row.Value)
cmbItem.Selected = true;
cmb.Items.Add(cmbItem);
}
//If no item matched and cmbItem.Selected = true is not hit, it will still
//have selectedindex = 0 and show the first item, not the empty message