This is a migrated thread and some comments may be shown as answers.

[Solved] text keeps its value even if set_Cancel() is set to true

5 Answers 179 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Elisabeth
Top achievements
Rank 1
Elisabeth asked on 26 Nov 2012, 08:51 PM
I am trying to use the AutoCompleteBox control.

I only want to let the user select one instance of each token. My list of tokens contains "Test 1, "Test 2" and "Test 3".

I am using a function on the OnClientEntryAdding that checks whether the entry has already been added.
<script type="text/javascript">
    function entryAdding(sender, eventArgs) {
        var txt = sender.get_text();
        var entries = sender.get_entries(),
        count = entries.get_count();
        for (var i = 0; i < count; i++) {
            if (entries.getEntry(i).get_value() == eventArgs.get_entry().get_value()) {
                eventArgs.set_cancel(true);
            }
        }
    }
</script>

If I type in "Test" and select "Test 1", it's added to the list of tokens. If I type in "tes" and then select "Test 1" then the token is not added which is what I want. But if I then type in "tes" and select "Test 1" then the event is cancelled but the text in the control is "Test 1 ;tes". It seems that the text is still changed even if the entry has been cancelled. How can I prevent it from adding an invalid token?

If I save the entries, then it's ok because the collection of entries doesn't contain the invalid entry but I don't want to display that invalid entry.

Below is the setting of my control

<telerik:RadAutoCompleteBox runat="server" ID="ACBVoie" DataTextField="Desc_No_Voie" DataValueField="No_Voie" InputType="Token" width="100%" OnClientEntryAdding="entryAdding" AllowCustomToken="false">
</telerik:RadAutoCompleteBox >

Thanks




5 Answers, 1 is accepted

Sort by
0
Elisabeth
Top achievements
Rank 1
answered on 29 Nov 2012, 02:17 PM
Hi,

am I the only one with this problem?

Thanks
0
Nencho
Telerik team
answered on 29 Nov 2012, 04:45 PM
Hi Elisabeth,

I have performed some test, based on provided snippet of code and information, but I was unable to replicate the described issue. I have prepared a sample project for you, demonstrating the behavior at my end. In addition, I have recorded a video on the matter.

Regards,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Srimanta
Top achievements
Rank 1
answered on 18 Mar 2013, 02:20 PM
Hi,

I am also facing same, when I am trying to restrict duplicate entry to autocomplete text box. This is happening for ItemType = 'Token' .
When I am selecting token already selected from autocomplete text dropdown, normal text is adding along with existing Token for the same Token.

Please suggest any solution for the same.

My Codes:

                    <telerik:RadAutoCompleteBox ID="TextAutoComplete" Width="180" DropDownHeight="150" DropDownWidth="200" OnClientRequesting="OnAutoCompleteRequesting" 
                                OnClientEntryAdding="EntryAdding" runat="server">
                        <WebServiceSettings Method="GetRadCompletions" Path="../../Shared/WebServices/AutoCompleteExcelLikeFilter.asmx" />
                    </telerik:RadAutoCompleteBox>



And script is:

function EntryAdding(sender, eventArgs) {
    var entries = sender.get_entries();
    var count = entries.get_count();
    for (var i = 0; i < count; i++) {
        if (entries.getEntry(i).get_value() == eventArgs.get_entry().get_value()) {
            eventArgs.set_cancel(true);
        }
    }
}
0
Princy
Top achievements
Rank 2
answered on 19 Mar 2013, 05:25 AM
Hi,

Please try the following code snippet for avoiding duplicate entry of tokens. I have applied a small trick to avoid the duplication but the already added item appears at the last position in the AutoCompleteBox.

JavaScript:
<script type="text/javascript">
    function EntryAdding(sender, eventArgs) {
        var entries = sender.get_entries();
        var count = entries.get_count();
        for (var i = 0; i < count; i++) {
            if (entries.getEntry(i)._text == eventArgs.get_entry()._text) {
                alert("Item Already Added");
                var entry1 = sender.get_entries().getEntry(i);
                sender.get_entries().remove(entry1);
                break;
            }
        }
    }
</script>

Thanks,
Princy.
0
Srimanta
Top achievements
Rank 1
answered on 19 Mar 2013, 01:19 PM
Thanks Princy..
Tags
AutoCompleteBox
Asked by
Elisabeth
Top achievements
Rank 1
Answers by
Elisabeth
Top achievements
Rank 1
Nencho
Telerik team
Srimanta
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or