下拉列表indexchange事件不能正常工作

本文关键字:工作 常工作 indexchange 事件 不能 下拉列表 | 更新日期: 2023-09-27 18:19:06

我有4个下拉列表-学院,部门,分支和分部。我的代码应该是这样工作的,当我改变教师索引时,部门,分支和部门索引应该相应改变。其他3个也是一样。但它只工作,当我改变ddlFaculty下拉列表的索引,而不是其他下拉列表。自动回退适用于每个下拉列表。我所有的查询工作正常。我的代码是这样的:

protected void ddlFaculty_SelectedIndexChanged(object sender, EventArgs e)
{
    lblStatus.Text = "Faculty index changed...";
    lblStatus.Visible = true;
    ddlDept.Items.Clear();
    ddlBranch.Items.Clear();
    ddlDivison.Items.Clear();
    ddlDegree.Items.Clear();
    string connectionString = GlobalVariables.databasePath;
    SqlConnection sqlCon = new SqlConnection(connectionString);
    sqlCon.Open();
    string query = "select dp_name from DEPT where fac_id=(select fac_id from FACULTY where fac_name='" + ddlFaculty.SelectedValue + "')";
    SqlCommand cmd = new SqlCommand(query, sqlCon);
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        ddlDept.Items.Add(reader.GetValue(0).ToString());
    }
    reader.Close();
    sqlCon.Close();
}
protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
{
    lblStatus.Text = "Dept index changed...";
    lblStatus.Visible = true;
    ddlBranch.Items.Clear();
    ddlDivison.Items.Clear();
    string connectionString = GlobalVariables.databasePath;
    SqlConnection sqlCon = new SqlConnection(connectionString);
    sqlCon.Open();
    string query = "select br_name from BRANCH where dp_id=(select dp_id from DEPT where dp_name='" + strDeptName + "')";
    SqlCommand cmd = new SqlCommand(query, sqlCon);
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        ddlBranch.Items.Add(reader.GetValue(0).ToString());
    }
    reader.Close();
    sqlCon.Close();
}
protected void ddlBranch_SelectedIndexChanged(object sender, EventArgs e)
{
    lblStatus.Text = "Branch index changed...";
    lblStatus.Visible = true;
    ddlDivison.Items.Clear();
    string connectionString = GlobalVariables.databasePath;
    SqlConnection sqlCon = new SqlConnection(connectionString);
    sqlCon.Open();
    string query = "select dv_name from DIVISON where br_id=(select br_id from BRANCH where br_name='" + strBranchName + "')";
    SqlCommand cmd = new SqlCommand(query, sqlCon);
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        ddlDivison.Items.Add(reader.GetValue(0).ToString());
    }
    reader.Close();
    sqlCon.Close();
}

下拉列表indexchange事件不能正常工作

这里有一些实际的代码,让你更好地了解如何实现:重要提示:ddlFaculty AutoPostBack="true"

    public partial class WebForm1 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ddlFaculty.Items.Count == 0)
        {
            ddlFaculty.DataSource = GetFaculty();
            ddlFaculty.DataTextField = "LastName";
            ddlFaculty.DataValueField = "FacultyId";
            ddlFaculty.DataBind();
        }
    }

    private DataTable GetFaculty()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("FacultyId", typeof(int));
        dt.Columns.Add("LastName", typeof(string));
        var dr1 = dt.NewRow();
        dr1["FacultyId"] = 1;
        dr1["LastName"] = "Smith";
        dt.Rows.Add(dr1);
        var dr2 = dt.NewRow();
        dr2["FacultyId"] = 2;
        dr2["LastName"] = "Jones";
        dt.Rows.Add(dr2);
        return dt;
    }
    private DataTable GetDept(int facultyId)
    {
        var dt = new DataTable();
        dt.Columns.Add("DeptId", typeof(int));
        dt.Columns.Add("DeptName", typeof(string));
        switch (facultyId)
        {
            case 1:
                var dr = dt.NewRow();
                dr["DeptId"] = 1;
                dr["DeptName"] = "Administration";
                dt.Rows.Add(dr);
                break;
            case 2:
                var dr1 = dt.NewRow();
                dr1["DeptId"] = 2;
                dr1["DeptName"] = "Science";
                dt.Rows.Add(dr1);
                break;
        }
        return dt;
    }
    protected void ddlFaculty_SelectedIndexChanged(object sender, EventArgs e)
    {
        var selectedValue = ddlFaculty.SelectedValue;
        ddlDept.Items.Clear();
        ddlDept.DataSource = GetDept(Convert.ToInt16(selectedValue));
        ddlDept.DataTextField = "DeptName";
        ddlDept.DataValueField = "DeptId";
        ddlDept.DataBind();
    }       
}