Click here to Skip to main content
15,899,011 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
I want to Bind Dropdownlist from another dropdown without postback Is it Possible using a JAvascript?
Posted
Comments
Hemant Singh Rautela 10-Jan-14 3:32am    
why not use AJAX updatepanel with postback, So that it load only that part (both dropdownlist)... :-)
Member 10453691 10-Jan-14 3:34am    
I know that But there is problem for me to with ajax control. so. i cant handle ajax toolkit properly in my asp.net
Hemant Singh Rautela 10-Jan-14 3:39am    
What the issue...
& you can use JSON with webservice for it, You can google it...
:-)
Member 10453691 10-Jan-14 3:44am    
do you have any kind of link then pls give me.

You can do that using javascript but the problem is that when the page will postback (on button click) then it will create an issue.

The issue is event validation.
ASP.NET will try to match the control's option after and before postback and will throw an error as you have added new option elements to the dropdown.
 
Share this answer
 
Use this JavaScript example to populate a drop down. Call it from another dropdown onchange event:

XML
<script type="text/javascript">
       function populateDD2() {
           var dd1 = document.getElementById('<%=DD1.ClientID %>');
           var dd2 = document.getElementById('<%=DD2.ClientID %>');
           if (dd1.value == "Yes") {
               var options = ["Option 1", "Option 2", "Option 3"];
               for (var i = 0; i < options.length; i++) {
                   var opt = document.createElement("option");
                   opt.value = i;
                   opt.innerHTML = options[i];
                   dd2.appendChild(opt);
               }
           }
           else {
               dd2.options = null;// Or whatever else you might want to do.
           }
       }
   </script>


Controls:

XML
<asp:DropDownList runat="server" ID="DD1" onchange="populateDD2()">
            <asp:ListItem Text="- Select -"></asp:ListItem>
            <asp:ListItem Text="Yes"></asp:ListItem>
            <asp:ListItem Text="No"></asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList runat="server" ID="DD2">
        </asp:DropDownList>
 
Share this answer
 
Comments
njammy 10-Jan-14 4:39am    
Good point raised by Deviprasad. In this example you want to add options to a dropdown which changes the markup sent by the server to the client (event validation). The user can still select the option from the newly added values.

To overcome the event validation problem, depending on the complexity, you can store the values selected from the 'newly added' controls in a hidden field as JSON, or just any string value. On post back, the drop down will need to be rebuilt but you can access the hidden field values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900