Search code examples
asp.netautocompletetelerik

Asp.Net Telerik RadAutoCompleteBox DropDown List Showing items already selected


The RadAutoCompleteBox allows me to select items that were already selected. That way it allows the user to duplicate items selected.

I't happens the same way on that sample http://demos.telerik.com/aspnet-ajax/autocompletebox/examples/default/defaultcs.aspx

When i press 'n' and select Nancy. It goes to the box. Then i press 'n' and select Nancy again. Then i get 2 Nancyes on the box.

Is it expected? Can i prevent it from showing selected items on the Drop Down?

Thanks in advance.


Solution

  • You could prevent the items from displaying in the dropdown, by performing the appropriate query to the underlying datasource and excluding those fields, which matches to the text of the already added entries. For this purpose, you could use the OnDataSourceSelect event of the RadAutoCompleteBox :

    http://www.telerik.com/help/aspnet-ajax/autocompletebox-ondatasourceselect.html

    Another approach you could use is to prevent the selection of the certain Item, if an entry with the same text (or value) is already selected. This could be achieved at the OnClientEntryAdding client-side event of the control and by canceling its propagation if a match is found :

    <script type="text/javascript">
    
    function OnClientEntryAdding (sender, eventArgs) {
        var entries = sender.get_entries(),
        count = entries.get_count();   
    
        for (var i = 0; i < count; i++) {          
            if(entries.getEntry(i).get_text() == eventArgs.get_entry().get_text())
            {
                eventArgs.set_cancel(true);
            }
        }
    }
    </script>