Click here to Skip to main content
15,902,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to populated dropdownlist based on selection another dropdownlist using jquery in c#. There is my code, when i checked using firebug, it has return value but the dropdownlist not changed and the value was not filled into the dropdownlist destination. Can you tell me, what's wrong with my code.
Sory my language isn't good. i hope u know what i mean. Thanks.

i have 2 dropdownlist, ddBranch and ddEmployee
C#
<asp:DropDownList ID="ddBranch" style="width:206px" runat="server"> </asp:DropDownList> 
<br />
<asp:DropDownList ID="ddEmployee" style="width:206px" runat="server"> </asp:DropDownList> 



C#
<script type="text/javascript">
$('#<%=ddBranch.ClientID %>').change(function () {
            var branchID = $('#<%=ddBranch.ClientID%>').val()
            $('#<%=ddEmployee.ClientID %>').removeAttr("disabled");
            $('#<%=DropDownAO.ClientID %>').empty().append('<option selected="selected" value="0">Select Region</option>');
            $('#<%=DropDownAO.ClientID %>').attr('disabled', 'disabled');
            $.ajax({
                type: "POST",
                url: "form-detail.aspx/GetDataEmployee",
                data: "{'branchID':'" + branchID + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    var j = jQuery.parseJSON(msg.d);
                    var options;
                    for (var i = 0; i < j.length; i++) {
                        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>'
                    }
                    $('#<%=ddEmployee.ClientID %>').html(options)
                },
                error: function (data) {
                    alert('Something Went Wrong')
                }
            });
        });
</script>


There is the code behind
C#
[WebMethod]
        public static string GetDataEmployee(string branchID)
        {
            StringWriter builder = new StringWriter();
            DataSet ds = new DataSet();
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalConn"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("[dbo].[GetMOUNOData]", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@BranchID", branchID);
                    cmd.Parameters.AddWithValue("@Parameter", parameterData);
                    conn.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(ds);
                    conn.Close();
                }
            }

            DataTable dt = ds.Tables[0];
            builder.WriteLine("[");
            if (dt.Rows.Count > 0)
            {
                builder.WriteLine("{\"optionDisplay\":\"Select Region\",");
                builder.WriteLine("\"optionValue\":\"0\"},");
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    builder.WriteLine("{\"optionDisplay\":\"" + dt.Rows[i]["EmployeeName"] + "\",");
                    builder.WriteLine("\"optionValue\":\"" + dt.Rows[i]["EmployeeID"] + "\"},");
                }
            }
            else
            {
                builder.WriteLine("{\"optionDisplay\":\"Select Region\",");
                builder.WriteLine("\"optionValue\":\"0\"},");
            }
            string returnjson = builder.ToString().Substring(0, builder.ToString().Length - 3);
            returnjson = returnjson + "]";
            return returnjson.Replace("\r", "").Replace("\n", "");
                         
        }
Posted
Updated 31-May-15 3:53am
v2

1 solution

oh, my code is works, the issued is my css "chosen-select", why when i'm using the css then the dropdownlist doesn't filled by value, but when i delete the css then the dropdown list filled by value... anyone know ?
 
Share this answer
 

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