Hi! I need to create a new item to dropdownlist on client using CSOM. And then to make it selected. How can i make it work?
Hello segornak,
Thank you for posting in the community.
Adding an item to WebDropDown on the client could be achieved as such:
function addItem() { var dropDown = $find("WebDropDown1"); var newItem = dropDown.get_items().createItem(); newItem.set_text("New Item"); newItem.set_value(4); newItem.set_selected(true); dropDown.get_items().add(newItem); }
function addItem() {
var dropDown = $find("WebDropDown1");
var newItem = dropDown.get_items().createItem();
newItem.set_text("New Item");
newItem.set_value(4);
newItem.set_selected(true);
dropDown.get_items().add(newItem);
}
I am attaching the sample project I used to test this for your reference. Please note that for addItem () function an input type of button is being used. The reason is that with the current implementation AJAX request is made after every item is added trough the addItem() function on the client. Additinaly, using an ASP button would not be successful here due to the multiple requests which would be made to the server simultaneously.
Therefore adding multiple items simultaneously should be done serverside as such:
DropDownItem india = new DropDownItem(); india.Text = "India"; india.Value = "4"; india.Selected = true; WebDropDown1.Items.Add(india);
DropDownItem india = new DropDownItem();
india.Text = "India";
india.Value = "4";
india.Selected = true;
WebDropDown1.Items.Add(india);
Hope this helps. Feel free to contact me if you have any addtional questions regarding this matter.
What client event do I use to ensure it only fires once? I also have EnableMultiSelect=true set for a dropdownList (WebDropDown)