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?
What client event do I use to ensure it only fires once? I also have EnableMultiSelect=true set for a dropdownList (WebDropDown)
Hello segornak,
I am still following your case. Did you manage to resolve your issue?
Please do not hesitate to contact me if you are still experiencing any issues with this matter.
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.