ASP. Net application recursive algorithm case

Source: Internet
Author: User

ASP. Net application recursive algorithm case

Case requirement: dynamically arrange various departments of a company in the order of departments, dynamically generate a list on the page, and the sub-departments must be 20 PX more indented than the parent departments.

 

Page code:

 



Background code:

 

1,

 

    public class Department    {        public Department(int id, string name, int parentid)        {            this.Id = id;            this.Name = name;            this.ParentId = parentid;        }        public int Id { get; set; }        public string Name { get; set; }        public int ParentId { get; set; }        }

2,

 

 

List
 
  
ListTc = new List
  
   
(); Public void LoadData () {listTc. add (new Department (1, "Finance Department", 2); listTc. add (new Department (2, "corporate headquarters", 0); listTc. add (new Department (3, "Finance Group 1", 1); listTc. add (new Department (4, "Finance Group 2", 1); listTc. add (new Department (5, "R & D Department", 2); listTc. add (new Department (6, "R & D group 1", 5); listTc. add (new Department (7, "R & D group 2", 5); listTc. add (new Department (8, "R & D group 3", 5); listTc. add (new Department (9, "Business Department", 2); listTc. add (new Department (10, "Business Group 1", 9); listTc. add (new Department (11, "Business Group 2", 9); listTc. add (new Department (12, "Business Group 3", 9); listTc. add (new Department (13, "R & D group 1 first group", 6); listTc. add (new Department (14, "Business Group 1 first group", 10); listTc. add (new Department (15, "R & D group 1 second group", 6); listTc. add (new Department (16, "R & D group 1 second group 1", 15); listTc. add (new Department (17, "R & D group 1 second group 2", 15 ));}
  
 

3,

 

 

        List
 
   tempList = new List
  
   ();        public List
   
     Sort(int parentId)        {            var tcList = GetListTC(parentId);            if (tempList.Count < listTc.Count)            {                if (tcList.Count > 0)                {                    foreach (var tc in tcList)                    {                        if (!tempList.Contains(tc))                        {                            tempList.Add(tc);                            return Sort(tc.Id);                        }                    }                    var t = GetTC(parentId);                    return Sort(t.ParentId);                }                else                {                    var tc = GetTC(parentId);                    return Sort(tc.ParentId);                }            }            else            {                 return tempList;            }        }
   
  
 

4,

 

 

       public int GetLevel(Department tc,int level)        {            var t = listTc.Find(o => o.Id == tc.ParentId);            if (t!=null)            {                level++;                return GetLevel(t,level);            }            return level;        }        public List
 
   GetListTC(int parentId)        {             var list = listTc.FindAll(o =>o.ParentId == parentId);             return list;           }        public Department GetTC(int id)        {            var tc = listTc.Find(o => o.Id == id);            return tc;        }        public string InitHtml(int level, string name)        {            string html = string.Empty;            html += "
  
  •  

  • 5,
          public string CreateHtml()        {            var list = Sort(0);                   string strHtml = string.Empty;            foreach (var tc in list)            {                int lev = GetLevel(tc, 0);                strHtml += InitHtml(lev, tc.Name);            }            return strHtml;        }

    6,

     

     

           protected void btnCreate_Click(object sender, EventArgs e)        {            LoadData();            this.Literal1.Text = CreateHtml();        }


     

    Idea: sort the data first and generate li based on the number of levels of the current unit.

    Page:

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.