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

findItemByValue client-side function bug

3 Answers 46 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Tracy Dryden
Top achievements
Rank 1
Tracy Dryden asked on 24 Sep 2012, 08:58 PM
I've found a minor bug in the listbox client-side function findItemByValue. If you pass it an INTEGER with the value zero, it always returns null. Passing it any other integer (where an item with that value exists) will work correctly. The cause of the problem is that the function findItemByValue is coded as:
function(g){if(!g){return null;}...

When g is an integer 0, !g is true, so the function returns null. (When g is an integer 1, !g is false, when g is a string "0", !g is false.)

The work-around is simple, just don't pass an integer, but to work correctly and consistently, the function should check for "undefined" or null or "" instead of using !g, or it should check the type of the value passed first.

Since findItemByValue actually works correctly when a non-zero integer is passed, it took me a little work to figure out what the problem was. Hopefully this post will help others avoid the problem (I haven't checked, but I'd bet that the same problem occurs in other Telerik controls as well).

3 Answers, 1 is accepted

Sort by
0
Ivana
Telerik team
answered on 27 Sep 2012, 12:59 PM
Hi Tracy,

Thanks for the report.

Yes, indeed. I have tested this behavior at our end and it seems that your observations are correct. I have already logged an item for this issue in out Issue Tracking System and we will provide a fix for this behavior as soon as possible.

Meanwhile, you can use the following workaround:
Telerik.Web.UI.RadListBox.prototype.findItemByValue = function (theValue) {
    if (!theValue && theValue.toString()!="0") return null;
 
    var items = this.get_items();
    var itemsCount = items.get_count();
 
    for (var i = 0; i < itemsCount; i++) {
        if (items.getItem(i).get_value() == theValue) {
            return items.getItem(i);
        }
    }
 
    return null;
}
<telerik:RadListBox runat="server" ID="RadListBox1">
    <Items>
        <telerik:RadListBoxItem Text="item1" Value="0" />
        <telerik:RadListBoxItem Text="item2" Value="1" />
        <telerik:RadListBoxItem Text="item3" Value="2" />
    </Items>
</telerik:RadListBox>

Thanks again for your report. Your telerik points have been updated.

Kind regards,
Ivana
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
Tracy Dryden
Top achievements
Rank 1
answered on 27 Sep 2012, 01:33 PM
Thanks for the reply, and the workaround. I just converted the value to a string before calling findItemByValue.
0
Ivana
Telerik team
answered on 27 Sep 2012, 01:56 PM
Hi Tracy,

Yes, that would be another way to workaround this behavior. Thank you for sharing your solution.

Regards,
Ivana
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.
Tags
ListBox
Asked by
Tracy Dryden
Top achievements
Rank 1
Answers by
Ivana
Telerik team
Tracy Dryden
Top achievements
Rank 1
Share this question
or