Populating in codebehind & empty message

4 posts, 0 answers
  1. Mac
    Mac avatar
    36 posts
    Member since:
    Nov 2006

    Posted 22 May 2008 Link to this post

    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

  2. Rosi
    Admin
    Rosi avatar
    2527 posts

    Posted 22 May 2008 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Mac
    Mac avatar
    36 posts
    Member since:
    Nov 2006

    Posted 23 May 2008 Link to this post

    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.

  5. Helen
    Admin
    Helen avatar
    1052 posts

    Posted 26 May 2008 Link to this post

    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
Back to Top