Set text of databound combobox

12 posts, 1 answers
  1. Keith
    Keith avatar
    94 posts
    Member since:
    Jun 2012

    Posted 11 Jun 2013 Link to this post

    I have a combobox that is filled with a list of preset values. In the codebehind, I determine the selectedIndex value through a lookup which returns a value of 0 to ~. If the value cannot be found the codebehind function returns -1.

    Setting the selectedIndex to -1 merely selects the item at index 0 and this creates a problem for users who may save the data not knowing that the value was not found in the predefined list.

    So, what I would like to do is set the text of the combobox to the value that was not found, or if the search string was initially empty, set the text to "Select Item", however, this doesn't seem to be working.

    To make matters more complicated, I don't want to allow users to enter custom text because the idea is to get the users to select from the predefined values.

    If I add another item at index 0, then the library we are using to manage the lists will not work.

    How can I add a message similar to the EmptyMessage value, except this value would be as a prompt to select an item or the incorrect item itself that I can validate on postback.

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 12 Jun 2013 Link to this post

    Hi,

    I suppose you need to add a "Select item" as first item in RadCombobox programmatically .
    C#:
    RadCombobox1.Items.Insert(0, new RadComboBoxItem("Select Item", string.Empty));

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Keith
    Keith avatar
    94 posts
    Member since:
    Jun 2012

    Posted 13 Jun 2013 Link to this post

    That will invalidate the results from other functions.

    This needs to be similar to the value displayed from the EmptyMessage, only this would be displayed if no value is selected from the dropdown.
  5. Answer
    Nencho
    Admin
    Nencho avatar
    1458 posts

    Posted 18 Jun 2013 Link to this post

    Hello Keith,

    I would suggest you to use the DefaultItem of the RadComboBox, in order to achieve the desired functionality. Please refer to this documentation article, demonstrating how to use the DefaultItem.

    Regards,
    Nencho
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  6. Keith
    Keith avatar
    94 posts
    Member since:
    Jun 2012

    Posted 18 Jun 2013 Link to this post

    Very good, although there is a caveat, maybe even a bug, with using the default text that I didn't see addressed in the documentation.

    Whenever you set DefaultItem.Selected = True, selecting another item in the codebehind fails to select the item, however, you will not see the default text unless you disable the control.

    To reproduce
    1) Create a combobox and populate it with some items.
    2) Set the DefaultItem.Text and DefaultItem.Value
    3) Set DefaultItem.Selected = True
    4) Set ComboBox.SelectedItndex = 3 (or any valid indexed item EXCEPT item 0)

    At this point, note that the combobox displays Item 0

    5) Disable the combobox

    The combobox displays the default text

    6) Enable the combobox

    The combobox will now display item 0

    To work around this issue, before setting the SelectedIndex of the combobox, you must set DefaultItem.Selected = False

    This is due to the way the RadComboBox implements the default item. Unless this is the functionality that the Telerik team desires, I'd recommend setting the DefaultItem.Selected property to false whenever the programmer selects an item as the selected index.

    Also, please note that RadComboBox.DefaultItem has a type of Telerik.Web.UI.ComboBox.RadComboBoxDefaultItem and even though it is exposed publically, it is not accessible in the Visual Studios IDE and isn't reported as a class that can be instantiated.
  7. Nencho
    Admin
    Nencho avatar
    1458 posts

    Posted 21 Jun 2013 Link to this post

    Hello Keith,

    I was able to replicate the described problematic behavior and I will forward it to our developer team for further investigation. Thank you for pointing that out. You are correct that the DefaultItem is inherited from Telerik.Web.UI.ComboBox and you could instantiated using the Telerik.Web.UI.ComboBox lib :

    RadComboBoxDefaultItem defaultItem = new RadComboBoxDefaultItem();
            defaultItem.Text = "Def Item";
            defaultItem.Value = "Def Value";
            ComboBox1.DefaultItem = defaultItem;


    Regards,
    Nencho
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  8. Keith
    Keith avatar
    94 posts
    Member since:
    Jun 2012

    Posted 21 Jun 2013 Link to this post

    Just so we are clear. I was not able to get the RadComboBoxDefaultItem to show up in the intellisense for Visual Studios even though the library is referenced and whenever I tried to type it in, autocomplete would override it and fill in something else. I was able to overcome this, but others may see this as not being an available object.
  9. Nencho
    Admin
    Nencho avatar
    1458 posts

    Posted 26 Jun 2013 Link to this post

    Hello Keith,

    I had recorded a video for you, demonstrating the usage of the RadComboBoxDefaultItem at my end at code-behind and markup. Would you also record a video, demonstrating the problem at your end?

    Regards,
    Nencho
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  10. Keith
    Keith avatar
    94 posts
    Member since:
    Jun 2012

    Posted 26 Jun 2013 Link to this post

    Your own video shows the problem perfectly.

    Stop the video at 3 seconds. Take note that the list of items shown in the intellisense do not include RadComboBoxDefaultItem.

    When the video reaches 5 seconds, you will note that the RadComboBoxDefaultItem is nearly fully typed, yet it does not show in the remaining filtered list in the intellisense.

    As I said previously, this is not an issue where the control cannot be used, but rather an issue where the control is not accessible via the intellisense and users may experience issues creating the control - as I did initially.

    Incidently there is another bug in the control that may be related to the original bug I reported. The control does not fire a click event if the default item is visible and the user selects the top item in the combobox that is not the default item.
    Steps to recreate:
    1) Populate a radcombobox in codebehind with multiple items (for example: "apples" "oranges" "bananas")
    2) Set a default item and set selected = true (example: "Select Fruit")

    When the page is rendered, "Select Fruit" is displayed.
    Select "apples" from the dropdown.

    On postback, the SelectedIndexChanged event does not fire.

    Refresh the page so that "Select Fruit" is once again visible.
    Select "oranges" or "bananas" from the dropdown.

    On postback the SelectedIndexChanged event is fired.

    While this issue is being investigated and corrected, I have managed a workaround by checking the eventtarget of the postback and manually calling a function to do the work that was normally done in the SelectedIndexChanged event and not creating an event handler for the control.
  11. Nencho
    Admin
    Nencho avatar
    1458 posts

    Posted 01 Jul 2013 Link to this post

    Hello Keith,

    I followed the provided instructions, trying to replicate the described problem, but to no avail. Here is a video, demonstrating the behavior at my end. Please take a look, and let me know if I had missed something. In addition, I had tested with the version described in the ticket info - 2013.1 417 and with our latest official release - 2013 Q2. Please let me know if the used version at your end is different.

    Regards,
    Nencho
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  12. Marcos
    Marcos avatar
    5 posts
    Member since:
    Feb 2015

    Posted 24 Feb 2015 in reply to Nencho Link to this post

    Hi Nencho,

    Then what is the solution? I do have the same problem than Keith.
  13. Nencho
    Admin
    Nencho avatar
    1458 posts

    Posted 27 Feb 2015 Link to this post

    Hello Marcos,

    I noticed that you have submitted another ticket on the same topic in the other forum thread, where I have already post a reply :

    http://www.telerik.com/forums/combobox-selectedindexchanged-event-not-fired

    I would like to ask you to continue the conversion in the other forum thread, in order to avoid duplication.

    Regards,
    Nencho
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017