asp.net DropDownList中內容偽樹狀展示

暖楓無敵發表於2012-03-08
        #region "繫結區域下拉選單"
        /// <summary>
        /// 繫結樹形節點
        /// </summary>
        public void BindAreaDropDownList()
        {
            DataTable dt = new AreaBLL().GetData("", " Orderby ASC");
            Areaddl.Items.Add(new ListItem("請選擇", "0"));
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    if (!DBNull.Value.Equals(row["ParentID"]) && row["ParentID"].ToString() == "0")//繫結根節點
                    {
                        this.Areaddl.Items.Add(new ListItem(row["Name"].ToString(), row["ID"].ToString()));
                        this.bindDropChildItem(this.Areaddl, dt, row["ID"].ToString(), 1);
                    }
                }
            }
        }


        /// <summary>
        /// 繫結子節點
        /// </summary>
        /// <param name="drop">下拉選單</param>
        /// <param name="arealist">查詢到的資料集</param>
        /// <param name="id">當前的父節點</param>
        /// <param name="length">長度</param>
        public void bindDropChildItem(DropDownList drop, DataTable dt, string id, int length)
        {
            DataRow[] rows = dt.Select("ParentID='" + id + "'", " orderby ASC");//取出id子節點進行繫結
            for (int i = 0; i < rows.Length; i++)
            {
                this.Areaddl.Items.Add(new ListItem(SpaceLength(length) + rows[i]["Name"].ToString(), rows[i]["ID"].ToString()));
                this.bindDropChildItem(drop, dt, rows[i]["ID"].ToString(), length + 1);//空白數目加1
            }
        }

        /// <summary>
        /// 子節點前面的空白數
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        protected string SpaceLength(int i)
        {
            string space = "";
            for (int j = 0; j < i; j++)
            {
                space += " ";//注意這裡的空白是智慧abc輸入法狀態下的v11字元;
            }
            return space;
        }

        #endregion



相關文章