validate listbox items client side code

5 posts, 1 answers
  1. arturo
    arturo avatar
    6 posts
    Member since:
    Jan 2011

    Posted 18 May 2011 Link to this post

    hi all

    i have a little problem with radlistbox, already i add items from a radcombobox, this function run correctly, but radlistbox accept repeated items, i want to know how i can validate with client side code if items on list do repeated before to add new item

    thanks in advance for read this thread and for all answers

    Regards!

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 19 May 2011 Link to this post

    Hello Arturo,

    RadListBox provides a flexible client-side API. You can easily interact with the listbox in the browser using its client-side object.
    You get the total items by get_items() method and loop through it and check it with the newly added items.

    Check out the following help article for more on client side API.
    RadListBox client API

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. arturo
    arturo avatar
    6 posts
    Member since:
    Jan 2011

    Posted 19 May 2011 Link to this post

    hi shinu, thanks for reply my question, but i have a problem with the code i've wrote, the insert of new item proceed correctly, but the loop where i count the items collection of radlistbox not execute correctly, the IE not throw me an error of client side code, i guess i'm doing something wrong

    this is my code


    <script type="text/javascript">
             
               function AddNewItem() {
               var lb = $find("<%= lstusers.ClientID %>"); // search radlistbox
               var itemtext = $find("<%= cmbusers.ClientID %>").get_value(); //selected item from a combobox
               if (!itemtext) {
                         alert("Please select a user to add in the group.");
                         return false;
                     }                        
    //this is the loop that use to count the radlistbox items and validate they items not repeat
               for (i=0;i<=lb.get_items().count;i++){
                    if (lb.getItem[i].get_value() = itemtext){
                        alert("this user already exists in the list");
                        return false;
                    }
               }
                 
               var item = new Telerik.Web.UI.RadListBoxItem();
               item.set_text(itemtext);
               lb.trackChanges();
               lb.get_items().add(item);
               item.select();
               lb.commitChanges();
           }
  5. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 May 2011 Link to this post

    Hello Arturo,

    I have made some changes in your code.

    Javascript:
    <script type="text/javascript">
        function AddNewItem()
     {
            var lb = $find("<%= lstusers.ClientID %>");
            var itemtext = $find("<%= cmbusers.ClientID %>").get_value();
            if (!itemtext)
            {
                alert("Please select a user to add in the group.");
                return false;
            }
            for (i = 0; i < lb.get_items().get_count(); i++) {
                if (lb.getItem(0).get_value() == itemtext) {
                    alert("this user already exists in the list");
                    return false;
                }
            }
            var item = new Telerik.Web.UI.RadListBoxItem();
            item.set_text(itemtext);
            lb.trackChanges();
            lb.get_items().add(item);
            item.select();
            lb.commitChanges();
        }
    </script>

    Also take a look at the following help article for more on on RadListBox client side API.
    RadListBoxItem client API.

    Thanks,
    Shinu.
  6. arturo
    arturo avatar
    6 posts
    Member since:
    Jan 2011

    Posted 20 May 2011 Link to this post

    thx shinu!!

    its work perfectly!!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017