Adding Items Dynamically to combobox

3 posts, 1 answers
  1. Stanley
    Stanley avatar
    5 posts
    Member since:
    Dec 2013

    Posted 17 Jul 2014 Link to this post

    Hi Gents,

    I am currently experiencing the following problem.

    I allow a user to dynamically add values to a dropdown using the following.

    1.<asp:DropDownList ID="ddlPrevelantHomeLanguage" runat="server" DataValueField="LanguageID"
    2.                               DataTextField="Description" />


    01.// Add Language function
    02.            function OnClientCloseLanguage(oWnd, args) {
    03.                //get the transferred arguments
    04.                var arg = args.get_argument();
    05.                if (arg) {
    06.                    var language = arg.language;
    07. 
    08.                    var data = JSON.stringify({
    09.                        language: language
    10.                    });
    11. 
    12.                    $.ajax({
    13.                        type: "POST",
    14.                        url: "Source.aspx/AddLanguage",
    15.                        data: data,
    16.                        contentType: "application/json; charset=utf-8",
    17.                        dataType: "json",
    18.                        success: function (msg) {
    19.                            var result = msg.d;
    20.                            var langaugeDropDown = $('#<%= ddlPrevelantHomeLanguage.ClientID %>');
    21.                            langaugeDropDown.append($('<option value="' + result + '">' + language + '</option>'));
    22.                        },
    23.                        error: function (xhr, error) {
    24.                            radalert("<strong>An Error occured when adding the language.</strong>", 225, 100, "Error", "");
    25.                        }
    26.                    });
    27.                }
    28.            }

    This then goes to a web method which returns me the Id of the added language which i subsequently append to a dropdown on the page. This dropdown exists within a RadAjaxpanel

    01./// <summary>
    02.   /// Adds the language.
    03.   /// </summary>
    04.   /// <param name="language">The language.</param>
    05.   /// <returns></returns>
    06.   [WebMethod]
    07.   public static int AddLanguage(string language)
    08.   {
    09.       MarketingDao staticMarketingDao = DataAccessFactory.Create<MarketingDao>();
    10. 
    11.       Language entityLanguage = new Language();
    12.       entityLanguage.Description = language;
    13. 
    14.       staticMarketingDao.Insert<Language>(entityLanguage);
    15.       staticMarketingDao.SubmitChanges();
    16. 
    17.       return entityLanguage.LanguageId;
    18.   }

    However when i attempt to save my page, the selected value in the Language dropdown is "", i believe this is due to the server (viewstate) not knowing about the language that was added on the clientside.

    If i use the Telerik DropDownList control will i be able to overcome this problem as i need some assistance

    Kind Regards
  2. Answer
    Nencho
    Admin
    Nencho avatar
    1457 posts

    Posted 22 Jul 2014 Link to this post

    Hello Stanley,

    Yes, you could achieve the desired functionality with the RadDropDownList or the RadComboBox. To preserve changes, you must use the trackChanges and commitChanges methods and the newly added items (like any other changes) will be persisted after a postback.

    Regards,
    Nencho
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Stanley
    Stanley avatar
    5 posts
    Member since:
    Dec 2013

    Posted 01 Aug 2014 in reply to Nencho Link to this post

    Hi,

    Thank you very much, this worked great :)
Back to Top