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

Odd OnChange Behavior

4 Answers 131 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Chris
Top achievements
Rank 1
Chris asked on 02 Jan 2011, 09:24 PM
Found an oddity with the OnChange event and the select method.  Essentially when someone clicks on a selection, a confirm box checks to be sure if they want to navigate, and if not then it reselects the previous value.  The issue scenarios are as follows:

A -

1) Select an option.
2) Click 'Cancel' in the confirm dialog.
3) See that the previous option is showing in the combobox.
4) Select another option and note that the change event does NOT fire.  No idea why the onclick event is not firing.

B -

1) Select an option.
2) Click 'Cancel' in the confirm dialog.
3) Click elsewhere on the page and note that the change event DOES fire unexepectedly.  It appears that this is happening in the telerik.list.js in the 'mousedown' event handler that is setup in the initialize function.

Here is the code:

<%= Html.Telerik().ComboBox()
    .Name("TestSelect")
    .BindTo(Model.List)
    .SelectedIndex(Model.SelectedIndex)
    .ClientEvents(events => events.OnChange("onChangeTest"))%>
  
<script type="text/javascript">
    function onChangeTest(e) {
        var el = $("#TestSelect").data("tComboBox");
        var $items = el.dropDown.$element.find('> .t-reset > .t-item');
        var value = el.value();
        var text = el.text();
        var previousIndex = <%: Model.SelectedIndex %>;
        if (confirm('Do you want to navigate?'))) {
            window.location.href = 'some url/' + id
        }
        else {
            el.select($items[previousIndex]);
        }
    }
</script>

4 Answers, 1 is accepted

Sort by
0
Chris
Top achievements
Rank 1
answered on 03 Jan 2011, 03:50 PM
Oops forgot to mention:

.NET 4.0
2010_3_1110 source
0
Georgi Krustev
Telerik team
answered on 03 Jan 2011, 04:18 PM
Hello Chris,

Because OnChange event is not cancellable pre-selecting item in the aforementioned event handler is not correct. Anyway you can overcome this limitation with the following code snippet:

function onChangeTest(e) {
    var el = $("#TestSelect").data("tComboBox");
    var previousIndex = <%: 0 %>;
    if (confirm('Do you want to navigate?')) {
        window.location.href = 'some url/' + id
    }
    else {
        setTimeout(function(){el.value(el.data[previousIndex].Value);}, 0);
    }
}

Regards,
Georgi Krustev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Chris
Top achievements
Rank 1
answered on 03 Jan 2011, 05:08 PM
Georgi,

I'll give that a try.  What about the B scenario?
0
Chris
Top achievements
Rank 1
answered on 05 Jan 2011, 02:58 PM
Georgi,

I tried the solution and it seems to resolve both the issues.
Tags
ComboBox
Asked by
Chris
Top achievements
Rank 1
Answers by
Chris
Top achievements
Rank 1
Georgi Krustev
Telerik team
Share this question
or