1. 级联复选框
关键词分两级,子级选中时父级自动选中;父级取消选中时子级自动取消选中;
由于checkboxlist的SelectedIndexChanged事件无法确定当前改变选择的复选框。因此采用HiddenField辅助完成。将改变
选择之前选中的复选框value值以逗号隔开存到里面。
2. 加载页面时初始化该值
代码
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] == null || )
{
this.hidCheckbox.Value = "";
}
if (Request.QueryString["id"] != null )
{
。。。。
if (dr1["KID"].ToString().Equals(CheckBoxList_keyword.Items[i].Value.ToString()))
{
CheckBoxList_keyword.Items[i].Selected = true;
hidCheckbox.Value += " " + dr["KID"].ToString() + ",";
}
。。。。
}
}
{
if (Request.QueryString["id"] == null || )
{
this.hidCheckbox.Value = "";
}
if (Request.QueryString["id"] != null )
{
。。。。
if (dr1["KID"].ToString().Equals(CheckBoxList_keyword.Items[i].Value.ToString()))
{
CheckBoxList_keyword.Items[i].Selected = true;
hidCheckbox.Value += " " + dr["KID"].ToString() + ",";
}
。。。。
}
}
3.改变选择时进行相应的操作。
代码
#region 关键词级联选择
protected void CheckBoxList_keyword_SelectedIndexChanged(object sender, EventArgs e)
{
string sOld = this.hidCheckbox.Value;
foreach (ListItem item in CheckBoxList_keyword.Items)
{
string sql = "select FirstID,KID from ServiceKey where KID='" + item.Value + "'";
SqlDataReader dr = db.GetReader(sql);
string ChildID = "";//子关键词
string ParentID = "";//父关键词
if (dr.Read())
{
ChildID = dr["KID"].ToString();
ParentID = dr["FirstID"].ToString();
}
dr.Close();
if (ChildID == ParentID)//当前为父关键词 {
string childs = ",";
if (!item.Selected && sOld.Contains(ChildID.Trim ()+","))//由选中变为不选中
{
sql = "select FirstID,KID from ServiceKey where FirstID='" + item.Value + "' and
FirstID!=KID";
dr = db.GetReader(sql);
while (dr.Read())
{
childs += dr["KID"].ToString() + ",";
}
dr.Close();
if (childs != ",")
{
foreach (ListItem child in CheckBoxList_keyword.Items)
{
if (childs.Contains (","+child .Value .Trim ()+","))
{
child.Selected = false;//子关键词自动变为未选中
}
}
}
}
}
else//当獭?前°为a子哩?关?键ü词洙?
{
if (item.Selected&&!sOld .Contains (ChildID ))//由未选中变为选中
{
foreach (ListItem parent in CheckBoxList_keyword.Items)
{
if (parent.Value.ToString() == ParentID)
{
parent.Selected = true;//父关键词自动选中 }
}
}
}
}
//重新构造该字符串
string sNew = "";
foreach (ListItem li in CheckBoxList_keyword.Items)
{
if (li.Selected)
sNew += " " + li.Value + ",";
}
this.hidCheckbox.Value = sNew;
}
#endregion
protected void CheckBoxList_keyword_SelectedIndexChanged(object sender, EventArgs e)
{
string sOld = this.hidCheckbox.Value;
foreach (ListItem item in CheckBoxList_keyword.Items)
{
string sql = "select FirstID,KID from ServiceKey where KID='" + item.Value + "'";
SqlDataReader dr = db.GetReader(sql);
string ChildID = "";//子关键词
string ParentID = "";//父关键词
if (dr.Read())
{
ChildID = dr["KID"].ToString();
ParentID = dr["FirstID"].ToString();
}
dr.Close();
if (ChildID == ParentID)//当前为父关键词 {
string childs = ",";
if (!item.Selected && sOld.Contains(ChildID.Trim ()+","))//由选中变为不选中
{
sql = "select FirstID,KID from ServiceKey where FirstID='" + item.Value + "' and
FirstID!=KID";
dr = db.GetReader(sql);
while (dr.Read())
{
childs += dr["KID"].ToString() + ",";
}
dr.Close();
if (childs != ",")
{
foreach (ListItem child in CheckBoxList_keyword.Items)
{
if (childs.Contains (","+child .Value .Trim ()+","))
{
child.Selected = false;//子关键词自动变为未选中
}
}
}
}
}
else//当獭?前°为a子哩?关?键ü词洙?
{
if (item.Selected&&!sOld .Contains (ChildID ))//由未选中变为选中
{
foreach (ListItem parent in CheckBoxList_keyword.Items)
{
if (parent.Value.ToString() == ParentID)
{
parent.Selected = true;//父关键词自动选中 }
}
}
}
}
//重新构造该字符串
string sNew = "";
foreach (ListItem li in CheckBoxList_keyword.Items)
{
if (li.Selected)
sNew += " " + li.Value + ",";
}
this.hidCheckbox.Value = sNew;
}
#endregion