zoukankan      html  css  js  c++  java
  • 两级关键词,复选框级联选择。借助hiddenField

    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() + ",";
                            }

                。。。。
                }
                
        }

       
    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
  • 相关阅读:
    k8s 静态pod
    k8s pod资源配额对调度的影响
    mysql分库,动态数据库切换
    【转】 一个C#中的webservice的初级例子(二)
    【转】UpdatePanel 简单实例
    Linux远程mount文件系统
    【转】一个C#中webservice的初级例子(一)
    javascript读写文件
    SilverLight插件检测
    C#读写共享文件夹
  • 原文地址:https://www.cnblogs.com/janes/p/1796582.html
Copyright © 2011-2022 走看看