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

Reading value from DropDownList after page load

3 Answers 1128 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
AP
Top achievements
Rank 1
Iron
Iron
Veteran
AP asked on 17 Feb 2016, 09:44 AM

I have a page, bound to a model, with several dropdownlistfor controls. These work ok, however, some items on the form only need to be shown depending on the values of other fields. I can do this using the change event on the dropdownlist without a problem.

However, I also need this logic to fire, once the page is initially loaded, (as the page is an edit form as well as a new record form). Using the $(document).ready() function, when I try to read the value from the dropdownlist, I get an undefined or null reference error, as it seems the dropdownlist hasn't yet been bound to the data.

I also get this error using the $(window).load() function.  How can I get the initial value of the dropdown list, once the page has loaded, so I can process the logic needed to show / hide the appropriate sections of the form?

 The code I have is:-

var dropdownlist = $("#Replacement").data("kendoDropDownList");
var dataItem = dropdownlist.dataItem();
var replacementVal = dataItem.Code;
 
if(replacementVal=='Y')
{
    $('#divReplacementPost').show();
}
else
{
    $('#divReplacementPost').hide();
}

Thanks

3 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 19 Feb 2016, 10:55 AM
Hello AP,

If the widgets use remote binding, then they will not have their value set (data item will not be available) until the data is retrieved. This is expected due to the asynchronous nature of the Ajax requests.

If this is the case (remote binding is used), then you will need to wait until the sources are loaded. One possible approach is to use Promises. Here is a Dojo demo that demonstrates this approach: How this would be implemented is entirely up to your personal preferences.

Regards,
Georgi Krustev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
AP
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 19 Feb 2016, 12:50 PM
Thanks. I wasn't aware this was an ajax call issue, as I'm binding directly to a model passed by the controller into the view. The drop-downs are getting their list data via ajax calls, so I'm assuming they don't return data until they've loaded their list data.
0
Georgi Krustev
Telerik team
answered on 23 Feb 2016, 11:38 AM
Hello AP,

My guess for the depicted issue was that an Ajax call has been made. Due to its async behavior, the dataItem will be empty.

Now, however, I understand that this is not the case. In order to proceed with the investigation, we will need more details about the current implementation. Would it be possible to send us a repro demo?

Regards,
Georgi Krustev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
DropDownList
Asked by
AP
Top achievements
Rank 1
Iron
Iron
Veteran
Answers by
Georgi Krustev
Telerik team
AP
Top achievements
Rank 1
Iron
Iron
Veteran
Share this question
or