我有4个下拉列表 - Faculty,Dept,Branch和Divison。我的代码是这样工作的,当我改变教师索引时,Dept,Branch和Divison索引应该相应地改变。对于其他3也是如此。但是只有当我改变ddlFaculty下拉列表的索引时才有效,而不是用于其他下拉列表。 Autopostback适用于每个下拉列表。我的所有查询都运行正常。我的代码是这样的..
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();
}
答案 0 :(得分:0)
这里有一些实际的代码可以让您更好地了解如何实现: 重要提示: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();
}
}