none
【ASP.NET】比较费解的下拉框树形菜单选中问题 RRS feed

  • 问题

  • 弄了好长时间最后还是不行,无奈上来请求高手帮助,代码如 下:

       protected void ItemBand()
        {
            using (SqlConnection con = OperateDB.GetConnection())
            {
                con.Open();

                DataTable datatable = OperateDB.GetDataSet("select * from Sk_Item order by sorder asc", CommandType.Text, "Sk_Item").Tables[0];
                DataRow[] row = datatable.Select("ParentID='0'");//从ParentID为0的开始
                //添加根目录
                ListItem li = new ListItem(row[0]["name"].ToString());
                this.item.Items.Add(li);
                addOtherDll("", Convert.ToInt32(row[0]["id"]), datatable, 1, item.ID);

                item.Items.Insert(0, new ListItem("", "0"));//插入列表第一项为空
                item.Items.Remove("菜单列表");

            }
        }

     

    /**/
        /// <summary>
        /// 添加其他节点
        /// </summary>
        /// <param name="Pading">空格</param>
        /// <param name="DirId">父路径ID</param>
        /// <param name="datatable">返回的datatable</param>
        /// <param name="deep">树形的深度</param>
        /// <param name="ConID">控件的ID</param>
        private void addOtherDll(string Pading, int Id, DataTable datatable, int deep, string ConID)
        {
            DataRow[] rowlist = datatable.Select("ParentID='" + Id + "'");
            foreach (DataRow row in rowlist)
            {
                string strPading = "";
                for (int j = 0; j < deep; j++)
                {

                    strPading += ((Id == 1) ? "" : Server.HtmlDecode("&nbsp;&nbsp;"));//如果ID为不1,添加空格

                }
                //添加节点
                ListItem li = new ListItem(strPading + (Id == 1 ? "" : "|--") +row["name"].ToString(), row["id"].ToString());


                //判断对应字段,加载颜色
                if (row["color"].ToString() != "")
                {
                    string color = row["color"].ToString(); //获取颜色
                    li.Attributes.Add("style", "color:" + color);
                }
              

                //判断为哪个控件绑定
                if (ConID == "item")
                {    
                    this.item.Items.Add(li);
                    addOtherDll(strPading, Convert.ToInt32(row["id"]), datatable, deep + 1, "item");
                }
                else
                {
                    this.department.Items.Add(li);
                    //递归调用addOtherDll函数,在函数中把deep加1
                    addOtherDll(strPading, Convert.ToInt32(row["id"]), datatable, deep + 1, "department");
                }

              

            }

          

        }

     

    显示效果如图:

    我想实现的效果是这样的:文本框中如果是“项目一” 或“项目二”那么下拉框默认显示第一项,文本框如果是111,下拉框则默认选中“项目一”    总之下拉框默认选择的都是文本框数据所在下拉框的父节点

     

    以上哪位朋友帮我看看啊,谢谢了!

    • 已移动 孟宪会Moderator 2010年7月2日 7:54 (发件人:.NET Framework 一般性问题讨论区)
    2010年6月30日 8:37

答案

  • 不说了,直接贴代码了

    using (SqlConnection con = OperateDB.GetConnection())
                {
                    SqlDataReader sdr = OperateDB.GetSqlDataReader("select * from Sk_Item where id='" + id + "'");
                    while (sdr.Read())
                    {
                        this.name.Text = sdr["name"].ToString().Trim();        //获取项目名称
                        this.sorder.Text = sdr["sorder"].ToString();   //获取排序序号
                        this.name.ForeColor = Color.FromName(sdr["color"].ToString());  //设置文本框字体颜色
                        ViewState["color"] = this.name.ForeColor.Name;//保存字体颜色




                        //绑定下拉框
                        DataTable dtable = OperateDB.GetDataSet("select * from Sk_Item order by sorder asc", CommandType.Text, "Sk_Item").Tables[0];
                        DataRow[] rows = dtable.Select("ParentID='1'");
                        //添加根目录
                        foreach (DataRow row in rows)
                        {
                            ListItem li = new ListItem(row["name"].ToString(), row["id"].ToString());

                            //判断对应字段,加载颜色
                            if (row["color"].ToString() != "")
                            {
                                string color = row["color"].ToString(); //获取颜色
                                li.Attributes.Add("style", "color:" + color);
                            }


                            try
                            {
                                if (sdr["name"].ToString() == Convert.ToString(li.Text))//说明为父节点
                                {
                                    this.item.Items.FindByValue("0").Selected = true; //选择下拉框第一项
                                }
                                else
                                {
                                    string selectText = Convert.ToString(OperateDB.Do_ExecuteScalar("select id from Sk_Item where id='" + sdr["parentID"] + "'"));//获取对应父节点的名字

                                    this.item.Items.FindByValue(selectText).Selected = true;
                                }

                            }
                            catch
                            {
                                //人为添加异常,用来处理选中父节点时,该节点没有索引0而引发的异常。与选中子节点所引发的异常
                               
                            }




                            this.item.Items.Add(li);
                            addOtherDll("", Convert.ToInt32(row["id"]), dtable, 1, item.ID); //row["id"] 为ID字段
                        }



                        item.Items.Insert(0, new ListItem("", "0"));//插入列表第一项为空
                      

                      
                    }

                    sdr.Close();
                }

     

    关键是字体加粗部分,结贴

    • 已标记为答案 jinwb1982 2010年6月30日 13:47
    2010年6月30日 13:46