未在SelectedIndexChanged事件上加载级联Dropdownlist

时间:2018-12-27 09:43:09

标签: c# asp.net .net webforms

我有两个名为ddFatherEmployeeNumber和ddEmployeeFatherName的下拉列表。现在在加载事件中,我正在从数据库中填充ddFatherEmployeeNumber。这是代码

private DataTable LoadComboBoxFatherEmployeeNumber()
    {
        DataTable dtFatherENo = new DataTable();
        using (SqlConnection con = new SqlConnection(Base.GetConnection))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT Id, EmployeeNo FROM TableFatherMaster", con))
            {
                cmd.CommandType = CommandType.Text;
                con.Open();
                SqlDataReader r = cmd.ExecuteReader();
                dtFatherENo.Load(r);
            }
        }
        return dtFatherENo;
    }

加载事件代码为

if (!IsPostBack)
        {
            ddFatherEmployeeNumber.DataSource = LoadComboBoxFatherEmployeeNumber();
            ddFatherEmployeeNumber.DataTextField = "EmployeeNo";
            ddFatherEmployeeNumber.DataValueField = "Id";
            ddFatherEmployeeNumber.DataBind();
            ddFatherEmployeeNumber.Items.Insert(0, new ListItem("Select Father Employee No", "0"));
        }

这是ddFatherEmployeeNumber的.aspx代码

<div class="col-8">
                                    <asp:DropDownList ID="ddFatherEmployeeNumber" runat="server" class="form-control here" AutoPostBack="True" OnSelectedIndexChanged="ddFatherEmployeeNumber_SelectedIndexChanged"></asp:DropDownList>
                                </div>

现在,我想基于ddFatherEmployeeNumber选定值填充ddEmployeeFatherName。我的意思是,当用户从ddFatherEmployeeNumber列表中然后从数据库中选择员工编号时,该员工的姓名将加载并填充ddEmployeeFatherName。为此,我已经在ddFatherEmployeeNumber_SelectedIndexChanged事件上编写了代码。这是代码。

 protected void ddFatherEmployeeNumber_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddEmployeeFatherName.SelectedIndex == 0)
        {

        }
        else
        {
            using (SqlConnection con = new SqlConnection(Base.GetConnection))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT Id, Name FROM TableFatherMaster WHERE EmployeeNo=@EmployeeNo ", con))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@EmployeeNo", ddFatherEmployeeNumber.SelectedValue);
                    con.Open();
                    SqlDataReader r = cmd.ExecuteReader();
                    ddEmployeeFatherName.DataSource = r;
                    ddEmployeeFatherName.DataBind();
                }
            }
        }

    }

现在的问题是,它没有将数据加载到ddEmployeeFatherName中。 这是清晰了解的图片 enter image description here

1 个答案:

答案 0 :(得分:0)

首先尝试为DataTextField设置DataValueFieldddEmployeeFatherName属性,因为在使用DataBind()之前没有设置它们:

using (SqlConnection con = new SqlConnection(Base.GetConnection))
{
    using (SqlCommand cmd = new SqlCommand("SELECT Id, Name FROM TableFatherMaster WHERE EmployeeNo=@EmployeeNo ", con))
    {
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@EmployeeNo", ddFatherEmployeeNumber.SelectedValue);
        con.Open();
        SqlDataReader r = cmd.ExecuteReader();
        ddEmployeeFatherName.DataSource = r;

        // set text and value fields
        ddEmployeeFatherName.DataTextField = "Name";
        ddEmployeeFatherName.DataValueField = "Id";
        ddEmployeeFatherName.DataBind();
    }
}

如果dropdownlist选项值仍然为空,请尝试将SqlDataReader的内容加载到DataTable中,并使用它:

using (SqlCommand cmd = new SqlCommand("SELECT Id, Name FROM TableFatherMaster WHERE EmployeeNo=@EmployeeNo ", con))
{
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@EmployeeNo", ddFatherEmployeeNumber.SelectedValue);
    con.Open();
    SqlDataReader r = cmd.ExecuteReader();

    DataTable dt = new DataTable();
    dt.Load(r);

    ddEmployeeFatherName.DataSource = dt;
    ddEmployeeFatherName.DataTextField = "Name";
    ddEmployeeFatherName.DataValueField = "Id";
    ddEmployeeFatherName.DataBind();
}