Adding empty string to RadComboBox
-
12-09-2019 - |
Question
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?
Solution
Seems like the accepted answer on Telerik says that you use client side script to prevent the text editing.
<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>
OTHER TIPS
RadComboBox1.Items.Insert(0, New RadComboBoxItem("Select a continent"))
This will add "Select a continent" as the first item in the combobox.
just put this
ComboBox.Text = String.Empty
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();
Is 'AppendDataBoundItems' set to true?
Another option is to add the item to the combobox right after binding, and then setting it as selected.
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.