This is a migrated thread and some comments may be shown as answers.

Populating in codebehind & empty message

3 Answers 224 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Mac
Top achievements
Rank 2
Mac asked on 22 May 2008, 11:08 AM

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

3 Answers, 1 is accepted

Sort by
0
Rosi
Telerik team
answered on 22 May 2008, 04:22 PM
Hello Mac,

I suggest you try setting the AllowCustomText property to true. This should help.

Regards,
Rosi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Mac
Top achievements
Rank 2
answered on 23 May 2008, 07:26 AM
Indeed setting the allowcustomtext property does show the empty message but at the expense of allowing text to be entered which is not what I'm after hence the code -

//Custom or fixed list?
cmb.AllowCustomText = Custom;
if (Custom)
    cmb.EmptyMessage = "Select a value or enter your own";
else
    cmb.EmptyMessage =
"Select a value";

It just seems odd that there is no possible way to not have the combobox show the emptymessage when you are not using allowcustomtext, short of adding a blank item at the beginning of item collection.

Thanks
Mac.

0
Helen
Telerik team
answered on 26 May 2008, 10:18 AM
Hello,

We've implemented the EmptyMessage when the AllowCustomText property is set to true. Otherwise, when the AllowCustomText property is set to false, there is always one selected item(usually the first item) and it contradicts to an empty message itself.

We are sorry for the inconvenience it might have caused you.

Regards,
Helen
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
ComboBox
Asked by
Mac
Top achievements
Rank 2
Answers by
Rosi
Telerik team
Mac
Top achievements
Rank 2
Helen
Telerik team
Share this question
or