且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据另一个下拉列表中的选择填充一个Asp.net下拉列表

更新时间:2022-10-17 18:34:07

' #<%= ddBranch.ClientID%>')。change(function(){
var branchID =


' #<%= ddBranch.ClientID% >')。val()


' # <%= ddEmployee.ClientID%>')。removeAttr( disabled );

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

<asp:DropDownList ID="ddBranch" style="width:206px" runat="server"> </asp:DropDownList> 
<br />
<asp:DropDownList ID="ddEmployee" style="width:206px" runat="server"> </asp:DropDownList> 




<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

[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", "");
                         
        }

('#<%=ddBranch.ClientID %>').change(function () { var branchID =


('#<%=ddBranch.ClientID%>').val()


('#<%=ddEmployee.ClientID %>').removeAttr("disabled");