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?
Adding empty string to RadComboBox
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>
RadComboBox1.Items.Insert(0, New RadComboBoxItem("Select a continent"))
This will add "Select a continent" as the first item in the combobox.
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 –
Premeditation
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
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?
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
Another option is to add the item to the combobox right after binding, and then setting it as selected.
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
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.
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.