findItemByValue client-side function bug

4 posts, 0 answers
  1. Tracy Dryden
    Tracy Dryden avatar
    34 posts
    Member since:
    Dec 2005

    Posted 24 Sep 2012 Link to this post

    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).

  2. Ivana
    Admin
    Ivana avatar
    657 posts

    Posted 27 Sep 2012 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Tracy Dryden
    Tracy Dryden avatar
    34 posts
    Member since:
    Dec 2005

    Posted 27 Sep 2012 Link to this post

    Thanks for the reply, and the workaround. I just converted the value to a string before calling findItemByValue.
  5. Ivana
    Admin
    Ivana avatar
    657 posts

    Posted 27 Sep 2012 Link to this post

    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.
Back to Top