Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
445
Add Item to DropdownList
posted

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?

Parents
No Data
Reply
  • 17590
    Suggested Answer
    Offline posted

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

    }

     

     

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

     

    Hope this helps. Feel free to contact me if you have any addtional questions regarding this matter.

     

    WDDAddItems.zip
Children