views:

2101

answers:

5

Guys,

I have a webpage that has a Telerik RadComboBox on the page. One of the properties of this ComboBox is EmptyMessage, which fills the combobox with a message when an item is not selected. I am binding my combobox to a datasource at runtime and for some reason, it wipes this EmptyMessage away. Is there a way to keep my data items in tact and have the empty message there too? And default it to the empty message?

A: 

Is 'AppendDataBoundItems' set to true?

asp316
Sadly this has nothing to do with it. EmptyMessage is only used when AllowCustomText is set to true, for some reason. It doesn't seem one can use the EmptyMessage style without allowing users to enter custom text.
Grank
A: 

Another option is to add the item to the combobox right after binding, and then setting it as selected.

Nick
That is ultimately what @te's answer provides.
rockinthesixstring
@rockinthesixstring Yeah, looks like he provided the simple code 4 months after this answer was posted...
Nick
A: 

I found the answer. For anyone curious or someone ever needs to do a similar things, you need to set the AllowCustomText property to True. This fixed my issue.

icemanind
This isn't a good solution in many cases, because it also allows a user to enter their own custom text, instead of forcing them to select one of the existing items. I would guess that in most cases, this type of behavior is not wanted.
bcwood
Allow custom text is not the right answer. Most of the time you only want them to be able to use the values you provide. Please see @te's answer for the "right" answer.
rockinthesixstring
How can you guys possible mark down my answer? I am the one asking the question. My answer fixed the issue I was having.
icemanind
I understand it "fixed" your issue but it doesn't solve the problem. As I said in my comment. If you don't actually want the end user to be able to enter custom text, then you need to insert a blank record to the beginning of your dropdown list. @te has done this masterfully, you just have to remember to do it after you bind your RadComboBox.
rockinthesixstring
+1  A: 
RadComboBox1.Items.Insert(0, New RadComboBoxItem("Select a continent"))

This will add "Select a continent" as the first item in the combobox.

te
The problem with this is that you don't get to use the EmptyMessage style. Why even use a RadComboBox then; unless you're using templates you might as well go back to good old asp:DropDownList. :P
Grank
This is way old I know, but @Grank - I use @te's method and it works wonders. You ask "why even use the RadComboBox" - it's for the wonderful autocomplete/autosuggest feature that you don't get with the good ol' DropDownList.
rockinthesixstring
A: 

Seems like the accepted answer on Telerik says that you use client side script to prevent the text editing.

Telerik forum page

<telerik:Radcombobox ID="RadComboBox1" runat="server" AllowCustomText="True" EmptyMessage="-please select one-">    
<Items>     
    <telerik:RadComboBoxItem runat="server" Text="Item1"></telerik:RadComboBoxItem>     
    <telerik:RadComboBoxItem runat="server" Text="Item2"></telerik:RadComboBoxItem>     
</Items>    

<script type="text/javascript"> 
function pageLoad() 
{ 
   var combo = $find("<%= RadComboBox1.ClientID %>"); 
   var input = combo.get_inputDomElement(); 
   input.onkeydown = onKeyDownHandler; 
} 
function onKeyDownHandler(e) 
{ 
  if (!e) 
  e = window.event;        
  e.returnValue = false; 
  if (e.preventDefault) 
  { 
    e.preventDefault(); 
  } 
} 
</script> 
starskythehutch