locked
Change Dropdownlist based on value returned in AJAX RRS feed

  • Question

  • User-1688352006 posted

    I am setting up a page to copy values from an existing record and paste them into a new record to save the user filling out a lengthy form with similar information.

    I have successfully been able to retrieve values and paste them into the fields for the text boxes. I am struggling with being able to get my drop down lists to change their values to match the copied record.

    The drop down lists get their values from a look up table within the database.

    The JS code I am using is:

        $.ajax({
            type: "POST",
            url: "WSHardware.asmx/GetHardware",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
    
                var jhardwareitems = response.d;
                $('#HardwareDetails').empty();
                $.each(jhardwareitems, function (index, jhardware) {
    
                    //Populating TextBoxes in InsertItemTemplate
                    $('input[id$=HWNameTextBox]').val(jhardware.HardwareName);
                    $('input[id$=ModelNoTextBox]').val(jhardware.ModelNo);
                    $('dropdown.options[id$=DropDownList_Kit]').val(jhardware.KitID); //<-- CANT GET THIS TO CHANGE VALUE IN MY DROPDOWNLIST
    
                 // Shortened for clarity

    Any ideas?

    Thursday, March 20, 2014 9:09 PM

Answers

  • User1208776063 posted

       $('dropdown.options[id$=DropDownList_Kit]').val(jhardware.KitID); //<-- CANT GET THIS TO CHANGE VALUE IN MY DROPDOWNLIST
    

    If you want to select a value in the DropDownList, you would have to use select in JQuery element such as 

    $('select[id$="Idfilterstring"]').val('My value');

    There is no tagName or element type as dropdown. Also, you will not be to find any element with Id selector on options as you must have provided the Id for Select control, not for each option. Also, you are missing quotes around DropDownList_Kit

     $('select[id$="DropDownList_Kit"]').val(jhardware.KitID);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 21, 2014 9:57 AM

All replies

  • User1208776063 posted

       $('dropdown.options[id$=DropDownList_Kit]').val(jhardware.KitID); //<-- CANT GET THIS TO CHANGE VALUE IN MY DROPDOWNLIST
    

    If you want to select a value in the DropDownList, you would have to use select in JQuery element such as 

    $('select[id$="Idfilterstring"]').val('My value');

    There is no tagName or element type as dropdown. Also, you will not be to find any element with Id selector on options as you must have provided the Id for Select control, not for each option. Also, you are missing quotes around DropDownList_Kit

     $('select[id$="DropDownList_Kit"]').val(jhardware.KitID);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 21, 2014 9:57 AM
  • User-1688352006 posted
     $('select[id$="DropDownList_Kit"]').val(jhardware.KitID);

    Well this kind of worked. But for some reason I had to hit F5 to refresh the page before the the select would change.

    I was hoping to trigger the change on the press of a button via Ajax. Which is currently working for my text boxes.

    Any ideas why I have to refresh to get this to work?

    Sunday, March 23, 2014 8:04 PM
  • User1208776063 posted

    why I have to refresh

    It seems like you are clearing content of a specific div in the success callback.

     $('#HardwareDetails').empty();

    Make sure you put a breakpoint at the dropdownlist value assignment line and debug and see if that breakpoint is hit when button is clicked. 

    I was hoping to trigger the change on the press of a button via Ajax. Which is currently working for my text boxes.

    That's kinda wierd. But, it could be some issue with process how dynamic controls are being created.  If that logic is working after page refresh, please compare that sequence with button click action sequence and see if you find any difference.

    Sunday, March 23, 2014 8:11 PM
  • User-1688352006 posted

    It seems like you are clearing content of a specific div in the success callback.
     $('#HardwareDetails').empty();
    Make sure you put a breakpoint at the dropdownlist value assignment line and debug and see if that breakpoint is hit when button is clicked. 
    

    #HardwareDetails is not related to this problem. It's a different Div.

    Make sure you put a breakpoint at the dropdownlist value assignment line and debug and see if that breakpoint is hit when button is clicked. 
    

    All of the text Boxes I have in the form are getting the values put into them .i.e.

    $('input[id$=HWNameTextBox]').val(jhardware.HardwareName);

    Works just fine.

    I'm sorry but I do noot understand what you mean by

    it could be some issue with process how dynamic controls are being created.  If that logic is working after page refresh, please compare that sequence with button click action sequence and see if you find any difference.
    
    

    I'm very much a noob to this.

    Sunday, March 23, 2014 8:46 PM