This is a migrated thread and some comments may be shown as answers.

Adding Items Dynamically to combobox

2 Answers 47 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Stanley
Top achievements
Rank 1
Stanley asked on 17 Jul 2014, 02:28 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Nencho
Telerik team
answered on 22 Jul 2014, 09:53 AM
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.

 
0
Stanley
Top achievements
Rank 1
answered on 01 Aug 2014, 06:35 AM
Hi,

Thank you very much, this worked great :)
Tags
Ajax
Asked by
Stanley
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Stanley
Top achievements
Rank 1
Share this question
or