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

set selected option of dropdownlist by value not by index

3 Answers 2817 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Henk Jelt
Top achievements
Rank 1
Henk Jelt asked on 21 Aug 2012, 01:36 PM

UPDATE
After two days of googling I found the solution in this example:
http://jsfiddle.net/krustev/9qZnp/ 

I adapted it to my own code, and it works perfectly.

KR

Henk Jelt

END UPDATE

Hi

I have two dropdown lists.
One contains a list of customers. 
The second should contain a list of products for the selected customer.
Both are populated by calls to json-webservices.
The selection process works fine via postback (I cannot get cascading dropdownlists working...).

It works like this:
I select an option in the first dropdownlist, which is part of a form.
Then I invoke a postback, retrieve the value of the selected item in the dropdownlist, and use this to dynamically adapt the json call to retrieve the data for the second dropdownlist.
This works fine: the second dropdownlist is populated ok.

The problem I have is that the selected item in the first dropdownlist gets lost.
I want it to come back but have no clue how.

I am working in an MVC environment, so I save the value in a viewdata-object, which I can re-use in the view.

Which steps do I need to take to have the selected option in view?

KR

Henk Jelt Hoving

3 Answers, 1 is accepted

Sort by
0
Mk
Top achievements
Rank 1
answered on 22 Oct 2012, 07:25 AM
You can done it by using:-

var dropdownlist = $("#dropdownlist").data("kendoDropDownList");

// selects item if its text is equal to "test" using predicate function
dropdownlist.select(function(dataItem) {
    return dataItem.text === "test";
});

// selects item if its value is equal to "test" using predicate function
dropdownlist.select(function(dataItem) {
    return dataItem.value=== "test";
});
0
Gary Davis
Top achievements
Rank 2
answered on 26 Mar 2013, 08:37 PM
Thanks - but use properties Text and Value instead of text and value.
0
Graham
Top achievements
Rank 2
Iron
Iron
answered on 10 Aug 2017, 04:08 PM

Thanks Mk.

 

A "gotcha" - the datasource data item field name will not be called "text" if you have given a name to the DataTextField of the dropdown list - you will need to use the name you have given it instead.

Tags
DropDownList
Asked by
Henk Jelt
Top achievements
Rank 1
Answers by
Mk
Top achievements
Rank 1
Gary Davis
Top achievements
Rank 2
Graham
Top achievements
Rank 2
Iron
Iron
Share this question
or