根据另一个下拉列表中的选择填充一个Asp.net下拉列表 [英] Populate One Asp.net Dropdown based on Selection in Another Dropdown

查看:64
本文介绍了根据另一个下拉列表中的选择填充一个Asp.net下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c#中使用jquery根据选择另一个下拉列表填充下拉列表。有我的代码,当我使用firebug检查时,它有返回值,但下拉列表没有更改,并且值未填充到下拉列表目标中。你能告诉我,我的代码出了什么问题。

Sory我的语言不好。我希望你知道我的意思。谢谢。



i有2个下拉列表,ddBranch和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( 禁用);
$(' #<%= DropDownAO.ClientID%>')。空()。append(' < option selected =selectedvalue =0>选择地区< /选项> ;');
$(' #<%= DropDownAO.ClientID%>')。attr (' 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)
},
错误:函数(数据){
alert(' 某事错了'
}
});
});
< / script >





背后有代码

 [WebMethod] 
public static string GetDataEmployee( string branchID)
{
StringWriter builder = new StringWriter();
DataSet ds = new DataSet();
使用(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings [ LocalConn]。ConnectionString))
{
using 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 \:\选择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 \:\选择地区\,);
builder.WriteLine( \optionValue \:\0 \}, );
}
string returnjson = builder.ToString()。Substring( 0 , builder.ToString()。长度 - 3 );
returnjson = returnjson + ];
return returnjson.Replace( \ r )。替换( \ n );

}

解决方案

' < span class =code-string>#<%= 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");


这篇关于根据另一个下拉列表中的选择填充一个Asp.net下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆