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

Edited items not available on server side

3 Answers 46 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Timothy
Top achievements
Rank 1
Timothy asked on 25 Dec 2014, 02:40 PM
I written some client side code that edits the Item Collection of a RadDropDownList.
The problem is that when I do a postback, the serverside only detects the original list.

I don't see what I've done wrong.
This is what I have so far:
var ddl = $find("<%=_ddlLidsIndicators.ClientID%>");
ddl.get_items().clear();
 
var selectItem = new Telerik.Web.UI.DropDownListItem();
selectItem.set_text("Select...");
ddl.get_items().add(selectItem);
 
$.each(data, function (index, value) {
    var newItem = new Telerik.Web.UI.DropDownListItem();
    newItem.set_text(value);
    newItem.set_value(index);
    ddl.get_items().add(newItem);
});
 
var otherItem = new Telerik.Web.UI.DropDownListItem();
otherItem.set_text("Other");
ddl.get_items().add(otherItem);
 
ddl.commitChanges();

 

3 Answers, 1 is accepted

Sort by
0
Bozhidar
Telerik team
answered on 26 Dec 2014, 07:40 AM
Hello,

You have to call the trackChanges() method of the DropDownList control before you make your modifications, and commitChanges() after the modifications. In your sample you are only calling commitChanges(), which is why it's not working. Here's the modified version:
var ddl = $find("<%=_ddlLidsIndicators.ClientID%>");
             
ddl.trackChanges();
 
ddl.get_items().clear();
 
var selectItem = new Telerik.Web.UI.DropDownListItem();
selectItem.set_text("Select...");
ddl.get_items().add(selectItem);
 
$.each(data, function (index, value) {
    var newItem = new Telerik.Web.UI.DropDownListItem();
    newItem.set_text(value);
    newItem.set_value(index);
    ddl.get_items().add(newItem);
});
 
var otherItem = new Telerik.Web.UI.DropDownListItem();
otherItem.set_text("Other");
ddl.get_items().add(otherItem);
 
ddl.commitChanges();


Regards,
Bozhidar
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
Timothy
Top achievements
Rank 1
answered on 30 Dec 2014, 03:38 PM
Thank you Bozhidar,
This is mostly working.

I've got a button that makes a postback.
On the serverside, I can see all the new items now..

The problem is that on the first postback, It doesn't detect the correct SelectedItem.
On the second postback, it does correctly see the SelectedItem.

Do you have any thoughts on what could cause this?
0
Plamen
Telerik team
answered on 02 Jan 2015, 06:50 AM
Hi Timothy,

You should set the selected item after you have added it to the items collection as for example in the code below:
var ddl = $find("<%=_ddlLidsIndicators.ClientID%>");
                var data = ["one", "two"];
                ddl.trackChanges();
 
                ddl.get_items().clear();
 
                var selectItem = new Telerik.Web.UI.DropDownListItem();
                selectItem.set_text("Select...");
                
                ddl.get_items().add(selectItem);
                selectItem.set_selected(true);
 
                var otherItem = new Telerik.Web.UI.DropDownListItem();
                otherItem.set_text("Other");
                ddl.get_items().add(otherItem);
 
                ddl.commitChanges();

Hope this will help you solve the issue.

Regards,
Plamen
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.

 
Tags
DropDownList
Asked by
Timothy
Top achievements
Rank 1
Answers by
Bozhidar
Telerik team
Timothy
Top achievements
Rank 1
Plamen
Telerik team
Share this question
or