locked
Cannot Change Asp.net DropDown Value Using jQuery RRS feed

  • Question

  • User931778073 posted

    On my VB.net web form there are a dropdownlist which displays Manufacturers names, a textbox for user to enter a YEAR (could use a dropdown instead), a Search button, a Save button, and a GridView. When the Search button is clicked, the Gridview will show Year, Manufacturer's name, Model name for each product that the specified Manufacturer owns.

    To the left of each Gridview row is an Edit link. I would like to be able to change the value of the Manufacturers dropdownlist when the Edit link is clicked. My Edit link is just a regular html anchor tag. Its only job is to grab the Manufacturers value in the Gridview in the same row and set the value of the Manufacturers dropdownlist. Normally I can change the value of a dropdown using jQuery but for some reason it's not working. 

    Please see my jQuery script and page markup below:

    <asp:GridView ID="ProductGridView" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="ProductGridView_SelectedIndexChanged1" DataKeyNames="AFFILIATE_CODE">
            <Columns>
                <asp:TemplateField>
                   <ItemTemplate>                           
                       <a href="#" class="lnkButton" style="color:blue">Edit</a>
                   </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Year" HeaderText="Year" />
                <asp:BoundField DataField="Manufacturer" HeaderText="Manufacturer" />
                <asp:BoundField DataField="Model" HeaderText="Model" />                     
           </Columns>
    </asp:GridView
    $("Table#ctl00_Content_ProductGridView > tbody tr").click(function () {                
         var make = $(':nth-child(1)', this).text();     
         $('#<%=ddlManufacturer.ClientID%>').val(make);  // Set ddlManufactuer value to the value of the make variable
    });

    Saturday, January 26, 2019 5:44 PM

All replies

  • User475983607 posted

    Your question is not clear as you have not described what's "not working".   I assume,  the jQuery selectors. 

    Please use the browser's dev tools (F12) to go through some basic troubleshooting iterations before  posting code on the forum and making a vague statement that the code does not work.  I would write the values to the console to see what's going on.  This a very quick and easy way to start debugging.  

    $("Table#ctl00_Content_ProductGridView > tbody tr").click(function () {  
    	console.log("Row click event fired!');
    	var make = $(':nth-child(1)', this).text(); 
        console.log(':nth-child(1) = ' + make);
    	
    	$('#<%=ddlManufacturer.ClientID%>').val(make);  // Set ddlManufactuer value to the value of the make variable
    	console.log('ddlManufacturer = ' + $('#<%=ddlManufacturer.ClientID%>').val);
    	
    });

    Saturday, January 26, 2019 8:37 PM
  • User931778073 posted

    Hi mgebhard,

    Thanks for responding. I think you misunderstood what I posted. The problem I had was I can not reset the value of the dropdown ddlManufacturer using jQuery. What I wanted to do was when I click the Edit link, I want to set the value of the ddlManufacturer dropdown box to the value of the Manufacturer which is in the same row as the clicked Edit link (each row of the Gridview has an Edit link column, a Manufacturer column, and a Year column).

    I already did what you had suggested to test what was going on even before posting my question. I tested using the alert and using Chrome Dev tool and found that the value of ddlManufacturer was NULL but I still could not figure out why. The code below gives me the name of the Manufacturer which is in the same row as the clicked Edit link. 

    var make = $(':nth-child(1)', this).text(); 

    Then I was able to get the name of the Manufacturer to show in the alert. However, when I try to set the value of ddlManufacturer to the value of the 'make' variable as shown below, it did not work.

    $('#<%=ddlManufacturer.ClientID%>').val(make);

    I also tried the code below and it did not work either.

    $('#<%=ddlManufacturer.ClientID%>').text(make)

    Saturday, January 26, 2019 10:57 PM
  • User475983607 posted

    Thanks for responding. I think you misunderstood what I posted.

    No, I understand.  I'm trying to get you to debug your code.

    I tested using the alert and using Chrome Dev tool and found that the value of ddlManufacturer was NULL but I still could not figure out why.

    Can you explain how the community is supposed to figure that out without the code?  

    Can you post the ddlManufacturer markup, code behind that populates the dropdown, all the JavaScript?

    Sunday, January 27, 2019 3:23 PM