DropDownList 递归绑定

39 篇文章 1 订阅
12 篇文章 0 订阅

要实现下面的效果:

首先可实现DropDownList:

     public void SetDDL(ref  System.Web.UI.WebControls.DropDownList ddl, string value)
        {
            if (ddl != null)
            {
                if (ddl.Items.FindByValue(value) != null)
                {
                    ddl.SelectedIndex = -1;
                    ddl.Items.FindByValue(value).Selected = true;
                }

            }
        }

 

具体实现的方法:

        public void BindAccountTree(ref  System.Web.UI.WebControls.DropDownList ddl, bool withEmptyItem)
        {
            int AccountID= -1;

            ddl.Items.Clear();
            Account rootAccount = AccountLogic.GetInstance().GetAccount(AccountID);
            ddl.Items.Add(new ListItem(rootAccount.Name, rootAccount.AccountID.ToString()));

            AddChild(rootAccount.AccountID, ddl, 0);

            if (withEmptyItem)
            {
                ListItem item = new ListItem("请选择...", "");
                ddl.Items.Insert(0, item);
            }
        }

 

GetAccount 方法:

 public Account GetAccount(int AccountID) //Get one Account
        {

            string sqlCommand = "SELECT * FROM Account WHERE AccountID=" + AccountID;

        }

AddChild方法:

 private void AddChild(int ParentID, DropDownList ddl, int Level)
        {
            string strSpace = "                    ";
            IList list = AccountLogic.GetInstance().FetchSubAccounts(ParentID, "Name");
            string strA = strSpace.Substring(0, Level + 1) + "|-";

            Account account = null;

            for (int i = 0; i < list.Count; i++)
            {
                account = (Account)list[i];

                ddl.Items.Add(new ListItem(strA + account.Name, account.AccountID.ToString()));

                if (Level > 20)
                    break;
                AddChild(account.AccountID, ddl, Level + 1);
            }
        }

FetchSubAccounts方法:

       public IList FetchSubAccounts(int AccountID, string Order)
        {
            IList Accounts = new ArrayList();
            Database db = DatabaseFactory.CreateDatabase();
            string sqlCommand = "SELECT * FROM Account ";

            sqlCommand += (sqlCommand.IndexOf("WHERE") > 0 ? " AND" : " WHERE") + " (Deleted = 0)";

            sqlCommand += " AND (ParentID=" + AccountID + ")";

            if (Order.Trim().Length > 0)
            {
                sqlCommand += " ORDER BY " + Order;//Title DESC, Href
            }
            DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
            try
            {
                using (IDataReader dr = db.ExecuteReader(dbCommand))
                {
                    while (dr.Read())
                    {
                        Accounts.Add(GetAccount(dr));
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.WriteLog(ex);
            }
            return Accounts;
        }


页面调用方法:

 private void BindParentAccount()
        {
            this.BindAccountTree(ref this.ddlAccount, true);
        }


数据库表结构:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值