C# 向 RadComboBox 添加空字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/966789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding empty string to RadComboBox
提问by Icemanind
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?
我有一个网页,页面上有一个 Telerik RadComboBox。此 ComboBox 的属性之一是 EmptyMessage,它在未选择项目时用消息填充组合框。我在运行时将我的组合框绑定到数据源,出于某种原因,它清除了这个 EmptyMessage。有没有办法让我的数据项保持完整并在那里也有空消息?并将其默认为空消息?
采纳答案by starskythehutch
Seems like the accepted answer on Telerik says that you use client side script to prevent the text editing.
似乎 Telerik 上接受的答案说您使用客户端脚本来防止文本编辑。
<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>
回答by Bill Martin
Is 'AppendDataBoundItems' set to true?
'AppendDataBoundItems' 是否设置为 true?
回答by Nick
Another option is to add the item to the combobox right after binding, and then setting it as selected.
另一种选择是在绑定后立即将项目添加到组合框,然后将其设置为选中状态。
回答by Icemanind
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.
我找到了答案。对于任何好奇或需要做类似事情的人,您需要将 AllowCustomText 属性设置为 True。这解决了我的问题。
回答by te.
RadComboBox1.Items.Insert(0, New RadComboBoxItem("Select a continent"))
This will add "Select a continent" as the first item in the combobox.
这将添加“选择一个大陆”作为组合框中的第一项。
回答by Syed Umar Ahmed
just put this
把这个
ComboBox.Text = String.Empty
回答by Pabitra Dash
In design time set EmptyMessage property.
在设计时设置 EmptyMessage 属性。
<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();