7

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?

Tony L.
  • 17,638
  • 8
  • 69
  • 66
Icemanind
  • 47,519
  • 50
  • 171
  • 296

7 Answers7

7

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
  • 3,328
  • 1
  • 25
  • 35
5
RadComboBox1.Items.Insert(0, New RadComboBoxItem("Select a continent"))

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

sth
  • 222,467
  • 53
  • 283
  • 367
te.
  • 51
  • 1
  • 2
  • 2
    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 Nov 16 '09 at 17:46
  • 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. – Chase Florell Jul 28 '10 at 00:50
1

just put this

 ComboBox.Text = String.Empty
Syed Umar Ahmed
  • 5,612
  • 1
  • 21
  • 23
1

In design time set EmptyMessage property.

<telerik:RadComboBox ID="ddlCategory" EmptyMessage="-Select-" runat="server" Width="120px" DropDownWidth="100px" AllowCustomText="true">
</telerik:RadComboBox>    

In run time following code works for me.

ddlCategory.Text = "";
ddlCategory.ClearSelection();
Pabitra Dash
  • 1,461
  • 2
  • 21
  • 28
0

Is 'AppendDataBoundItems' set to true?

Bill Martin
  • 4,825
  • 9
  • 52
  • 86
  • 1
    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 Nov 16 '09 at 17:45
0

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

Nick
  • 1,708
  • 14
  • 18
-3

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
  • 47,519
  • 50
  • 171
  • 296
  • 1
    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. – Brandon Wood Aug 06 '09 at 15:27
  • 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. – Chase Florell Jul 28 '10 at 00:53
  • 1
    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 Jul 28 '10 at 16:12
  • 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. – Chase Florell Jul 28 '10 at 19:16
  • 1
    @Chase Preventing users from entering custom text was not part of icemanind's criteria. It does answer the questions he asked. This should not be down-voted. Vote up a better answer instead. – xr280xr Oct 29 '13 at 16:09