Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,
i need to bind dropdown values based on value selected from another dropdown in asp.net, but with out doing postback.for that i have used javascript. i used enablepage methods function to retrive data from server side to javascript and all values are coming..but i am unable to bind those values to required dropdown.please help me how to solve this.
my code is as follows.

<asp:ScriptManager ID="scm1" runat="server" EnablePageMethods="true">
1st dropdown
====================
XML
<asp:DropDownList ID="ddlBranch" runat="server" onchange="BindEmployeeNames()" data-md-selectize>
                                        <asp:ListItem Value="0">Select</asp:ListItem>
                                    </asp:DropDownList>

Note:-for this ddl data is coming dynamically on page load.

2nd dropdown
========================
XML
<asp:DropDownList ID="ddlEmployeeNames" runat="server" data-md-selectize>
                                    </asp:DropDownList>


my javascript:-
=======================
XML
var ddlEmployeeNamesItems;
        function BindEmployeeNames() {
            //alert("k");
            var ddlBranchCtrl = document.getElementById("<%=ddlBranch.ClientID %>");
                ddlEmployeeNamesItems = document.getElementById("<%=ddlEmployeeNames.ClientID %>");
                ddlEmployeeNamesItems.options.length == 0;
                // alert(ddlBranchCtrl.options[ddlBranchCtrl.selectedIndex].value);
                PageMethods.FillEmpData(ddlBranchCtrl.options[ddlBranchCtrl.selectedIndex].value, OnSuccess);
            }
            function OnSuccess(response) {
                //alert(response);
                ddlEmployeeNamesItems.options.length = 0;
                AddOption("Select", "0");
                for (var i in response) {
                    //alert(response[i].Name + "------------" + response[i].Id);
                    AddOption(response[i].Name, response[i].Id);
                }
            }
            function AddOption(text, value) {
                var option = document.createElement('option');
                option.value = value;
                option.innerHTML = text;
                document.getElementById("<%=ddlEmployeeNames.ClientID %>").options.add(option);

            }



please help me to solve this...
Thanks.
Posted
Updated 15-Sep-15 3:52am
v3
Comments
F-ES Sitecore 15-Sep-15 10:05am    
You can't bind server controls via javascript. Use a normal <select> control and Request.Form to read the selected value.
[no name] 15-Sep-15 13:34pm    
Is your page method calling successfully?(Put a debugger point and wait to hit debugger. If hit then there is no problem with server method. Secondly check that you are dropdown value in FillEmpData().

After successfully data return from FillEmpData() put an alert in your javascript. Check it is throwing you error.

Check as per above mentioned and provide where you are getting stuck or error.
pavan_ kumar 16-Sep-15 0:43am    
yes..pagemethods is calling successfully and also all values are coming properly but there are not binding to asp dropdown control.
pavan_ kumar 16-Sep-15 0:56am    
the data binding to asp dropdown if i call on pageload, if i call from onchange event of another dropdown data is not binding, i am wondering about this. how can i achieve this??????
[no name] 16-Sep-15 1:09am    
Javascript is looping properly? Put an alert message and make sure it is displaying alert message.

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