I am trying to learn how to programmatically set the value in a WebDropDown object (on the client). Here is my test code:
function SetTo(value) { var combo = $find('<%= wddRoles.ClientID %>'); var items = combo.get_items(); var index = -1 console.log(items); // returning NULL for (var i = 0; i < items.length; i++) { var item = items[i]; if (item.get_element().get_value() == value) { index = i; } } console.log(index); combo.set_selectedItemIndex(index);}
As shown in the comments, the statement combo.get_items(); is returning null. I have checked the combo variable, and it is being set (i.e. it is not null).
Any thoughts on how to set the selectedIndex or selectedItem in a WebDropDown?
Hi Rob,
Yes you are absolutely right :
1. items.getLength() should be used. That's because items are loaded lazily, so length will be zero initially (no javascript objects for the items will be created so the internal length will be zero) . getLength() on the other hand knows the actual number of items, before they are created.
2. yep, getItem(i) should be used. same reason as 1)
3. yep, get_text(), get_value() and get_index() are functions of the drop down item ($IG.DropDownItem class)
4. yep
5. yep
Thanks very much for sharing your feedback and experience with the control !
Angel
Again, not sure why .get_items() is returning null, but I did find a few other problems with my code:
ROB
Thanks Angel.
Tried your code and also mine which binds the WebDropDown control to a data source. In both cases, your code worked as expected.
I am not sure why I was getting a null when I ran the .get_items() method. Perhaps it was some other javascript problem on my page. If I find an answer, I will post it here for others.
Cheers,
Hey Rob,
Thanks for the input. I have assembled a WebSite for you (attached), where i have a dropdown, and a button click (asp:Button with onClientClick set) , i am changing the selected item, as well as the actual current value in the input.
I hope this works fine for you. I have noticed that i am doing the first several lines of code in exactly the same way, but get_items() in my case returns the correct instance to the dropdown items collection, to be more precise, an object of type $IG.DropDownItemCollection).
Thanks,