【问题标题】:ASPxCombobox, Allow user input and dropdown selectionASPxCombobox,允许用户输入和下拉选择
【发布时间】:2011-03-13 20:19:01
【问题描述】:

我正在使用 devexpress ASPxComboBox,但是我想知道如何允许用户输入值(以防它不在列表中)或从下拉列表中选择。

举个例子就好了!

谢谢,

提姆

【问题讨论】:

    标签: c# javascript vb.net devexpress


    【解决方案1】:

    我编写了一个代码,如果按下 Enter 键,您可以将新项目添加到 ComboBox 的 Items 集合中。

       <script type="text/javascript">
        function findItemByText(editor, newText) {
            for(var i = 0; i< editor.GetItemCount(); i++)
                if(editor.GetItem(i).text == newText)
                    return true;
            return false;
        }   
    
        function tryAddNewItem(editor, newText) {
            if(!findItemByText(editor, newText))
                editor.AddItem(newText);
        }
       </script>
    

    ...

    <dx:ASPxComboBox ID="ASPxComboBox1" runat="server" DropDownStyle="DropDown" ValueType="System.String"
                Width="286px">
                <Items>
                    <dx:ListEditItem Text="Item 0" Value="0" />
                    <dx:ListEditItem Text="Item 1" Value="1" />
                </Items>
                <ClientSideEvents KeyPress="function(s,e) {
                    if(e.htmlEvent.keyCode == 13) 
                        tryAddNewItem(s, s.GetText());
                }"/>
    

    【讨论】:

    • 请注意DropDownStyle="DropDown" 而不是DropDownStyle="DropDownList",它允许输入新值。
    • 感谢您的回答! :)
    猜你喜欢
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 2022-01-13
    • 2017-02-08
    • 2022-01-03
    • 2014-10-26
    相关资源
    最近更新 更多