Adding empty string to RadComboBox
Asked Answered
P

7

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?

Philippic answered 8/6, 2009 at 20:21 Comment(0)
F
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> 
Fibrinous answered 11/5, 2010 at 12:40 Comment(0)
J
5
RadComboBox1.Items.Insert(0, New RadComboBoxItem("Select a continent"))

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

Jeanejeanelle answered 27/10, 2009 at 12:24 Comment(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. :PPremeditation
This is way old I know, but @Premeditation - 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.Turf
D
1

just put this

 ComboBox.Text = String.Empty
Deltoro answered 30/8, 2012 at 7:17 Comment(0)
M
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();
Muttonchops answered 29/9, 2016 at 11:21 Comment(0)
C
0

Is 'AppendDataBoundItems' set to true?

Crepe answered 8/6, 2009 at 20:30 Comment(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.Premeditation
H
0

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

Hauteloire answered 8/6, 2009 at 21:54 Comment(2)
That is ultimately what @te's answer provides.Turf
@rockinthesixstring Yeah, looks like he provided the simple code 4 months after this answer was posted...Hauteloire
P
-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.

Philippic answered 8/6, 2009 at 22:6 Comment(5)
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.Infant
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.Turf
How can you guys possible mark down my answer? I am the one asking the question. My answer fixed the issue I was having.Philippic
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.Turf
@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.Merozoite

© 2022 - 2024 — McMap. All rights reserved.