Reading value from DropDownList after page load

4 posts, 0 answers
  1. AP
    AP avatar
    187 posts
    Member since:
    Apr 2010

    Posted 17 Feb Link to this post

    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

  2. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 19 Feb Link to this post

    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
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. AP
    AP avatar
    187 posts
    Member since:
    Apr 2010

    Posted 19 Feb in reply to Georgi Krustev Link to this post

    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.
  5. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 23 Feb Link to this post

    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
Back to Top
UI for ASP.NET MVC is VS 2017 Ready