关于dropdownlist下拉框的三级联动和后台代码整合
我是一个刚接触开发没多久的新人。。= =最近用dropdownlist控件设置三级联动的时候,第一个和第二个联动的很正常,可是第二个和第三个联动的时候,第三个dropdownlist控件不管怎么样都一直为空,(正常是有数据的)下面是我的代码,谁能帮我去看下是哪边问题哦。
还有就是我觉得我现在后台这个联动的代码写的重复性太多,但是我不知道怎么整合好,能给我写个整合后的例子吗?
string strCon = ConfigurationManager.AppSettings["MsSql"];
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUserList();
ddlGroupIDBind();
ddlU_CNameBind();
ddlUserIDBind();
ddlDutyBind();
}
}
//部门下拉框
protected void ddlGroupIDBind()
{
string sqlSel = "select GroupID,G_CName from sys_Group where G_ParentID !=0 order by GroupID ";
SqlConnection con = new SqlConnection(strCon); ;
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlSel, con);
DataSet ds = new DataSet();
sda.Fill(ds);
ddlGroupID.DataSource = ds.Tables[0].DefaultView;
ddlGroupID.DataTextField = "G_CName";
ddlGroupID.DataValueField = "GroupID";
ddlGroupID.DataBind();
}
//员工姓名下拉框
protected void ddlU_CNameBind()
{
string sqlSel = "select UserID,U_CName from sys_User where U_GroupID='" + ddlGroupID.SelectedValue + "' order by UserID ";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlSel, con);
DataSet ds = new DataSet();
sda.Fill(ds);
ddlUCName.DataSource = ds.Tables[0].DefaultView;
ddlUCName.DataTextField = "U_CName";
ddlUCName.DataValueField = "UserID";
ddlUCName.DataBind();
}
//员工编号
protected void ddlUserIDBind()
{
string sqlSel = "select UserID from sys_User where U_CName='" + ddlUCName.SelectedValue + "' order by UserID ";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlSel, con);
DataSet ds = new DataSet();
sda.Fill(ds);
ddlUserID.DataSource = ds.Tables[0].DefaultView;
ddlUserID.DataTextField = "UserID";
ddlUserID.DataValueField = "UserID";
ddlUserID.DataBind();
}
//部门下拉框和姓名下拉框的二级联动触发事件
protected void ddlGroupID_SelectedIndexChanged(object sender, EventArgs e)
{
string U_GroupID = this.ddlGroupID.SelectedValue;
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand cmd = new SqlCommand("select UserID,U_CName from sys_User where U_GroupID='" + ddlGroupID.SelectedValue + "' order by UserID ", con);
SqlDataReader sdr = cmd.ExecuteReader();
this.ddlUCName.DataSource = sdr;
this.ddlUCName.DataTextField = "U_CName";
this.ddlUCName.DataValueField = "UserID";
this.ddlUCName.DataBind();
sdr.Close();
con.Close();
}
//姓名下拉框和员工编号下拉框的二级联动触发事件
protected void ddlUCName_SelectedIndexChanged(object sender, EventArgs e)
{
string UserID = this.ddlUCName.SelectedValue;
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand cmd = new SqlCommand("select UserID from sys_User where U_CName='" + ddlUCName.SelectedValue + "' order by UserID ", con);
SqlDataReader sdr = cmd.ExecuteReader();
this.ddlUserID.DataSource = sdr;
this.ddlUserID.DataTextField = "UserID";
this.ddlUserID.DataValueField = "UserID";
this.ddlUserID.DataBind();
sdr.Close();
con.Close();
}
//权限下拉框
protected void ddlDutyBind()
{
string sqlSel = "select ID,Duty from sys_Permission order by ID ";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlSel, con);
DataSet ds = new DataSet();
sda.Fill(ds);
ddlDuty.DataSource = ds.Tables[0].DefaultView;
ddlDuty.DataTextField = "Duty";
ddlDuty.DataValueField = "Duty";
ddlDuty.DataBind();
}
//权限等级下拉框
protected void ddlLvBind()
{
string sqlSel = "select ID,Lv from sys_Permission where Duty='" + ddlDuty.SelectedValue + "' order by ID ";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlSel, con);
DataSet ds = new DataSet();
sda.Fill(ds);
ddlLv.DataSource = ds.Tables[0].DefaultView;
ddlLv.DataTextField = "Lv";
ddlLv.DataValueField = "Lv";
ddlLv.DataBind();
}
//权限和权限等级二级联动触发事件
protected void ddlDuty_SelectedIndexChanged(object sender, EventArgs e)
{
string Duty = this.ddlDuty.SelectedValue;
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand cmd = new SqlCommand("select ID,Lv from sys_Permission where Duty='" + ddlDuty.SelectedValue + "' order by ID ", con);
SqlDataReader sdr = cmd.ExecuteReader();
this.ddlLv.DataSource = sdr;
this.ddlLv.DataTextField = "Lv";
this.ddlLv.DataValueField = "Lv";
this.ddlLv.DataBind();
sdr.Close();
con.Close();
}
------解决方案--------------------
Refer sample:
http://www.cnblogs.com/insus/archive/2012/10/16/2725307.html
http://www.cnblogs.com/insus/archive/2011/07/04/2097059.html
[解决办法]
你的dropdowmlist有没有加 AutoPostBack="True" 这个属性?
[解决办法]
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUserList();
ddlGroupIDBind();
ddlU_CNameBind();
ddlUserIDBind();
ddlDutyBind();
}
}
string strCon = ConfigurationManager.AppSettings["MsSql"];
//部门下拉框
protected void ddlGroupIDBind()
{
string sqlSel = "select GroupID,G_CName from sys_Group where G_ParentID !=0 order by GroupID ";
SqlConnection con = new SqlConnection(strCon); ;
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlSel, con);
DataSet ds = new DataSet();
sda.Fill(ds);
ddlGroupID.DataSource = ds.Tables[0].DefaultView;
ddlGroupID.DataTextField = "G_CName";
ddlGroupID.DataValueField = "GroupID";
ddlGroupID.DataBind();
}
//员工姓名下拉框
protected void ddlU_CNameBind()
{
string sqlSel = "select UserID,U_CName from sys_User where U_GroupID='" + ddlGroupID.SelectedValue + "' order by UserID ";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlSel, con);
DataSet ds = new DataSet();
sda.Fill(ds);
ddlUCName.DataSource = ds.Tables[0].DefaultView;
ddlUCName.DataTextField = "U_CName";
ddlUCName.DataValueField = "UserID";
ddlUCName.DataBind();
}
//员工编号
protected void ddlUserIDBind()
{
string sqlSel = "select UserID from sys_User where U_CName='" + ddlUCName.SelectedValue + "' order by UserID ";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlSel, con);
DataSet ds = new DataSet();
sda.Fill(ds);
ddlUserID.DataSource = ds.Tables[0].DefaultView;
ddlUserID.DataTextField = "UserID";
ddlUserID.DataValueField = "UserID";
ddlUserID.DataBind();
}
//部门下拉框和姓名下拉框的二级联动触发事件
protected void ddlGroupID_SelectedIndexChanged(object sender, EventArgs e)
{
ddlU_CNameBind();
}
//姓名下拉框和员工编号下拉框的二级联动触发事件
protected void ddlUCName_SelectedIndexChanged(object sender, EventArgs e)
{
ddlUserIDBind();
}
//权限下拉框
protected void ddlDutyBind()
{
string sqlSel = "select ID,Duty from sys_Permission order by ID ";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlSel, con);
DataSet ds = new DataSet();
sda.Fill(ds);
ddlDuty.DataSource = ds.Tables[0].DefaultView;
ddlDuty.DataTextField = "Duty";
ddlDuty.DataValueField = "Duty";
ddlDuty.DataBind();
}
//权限等级下拉框
protected void ddlLvBind()
{
string sqlSel = "select ID,Lv from sys_Permission where Duty='" + ddlDuty.SelectedValue + "' order by ID ";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(sqlSel, con);
DataSet ds = new DataSet();
sda.Fill(ds);
ddlLv.DataSource = ds.Tables[0].DefaultView;
ddlLv.DataTextField = "Lv";
ddlLv.DataValueField = "Lv";
ddlLv.DataBind();
}
//权限和权限等级二级联动触发事件
protected void ddlDuty_SelectedIndexChanged(object sender, EventArgs e)
{
ddlLvBind();
}