c#반복

2276 단어 귀속C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace family
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMajorNode();//binding major node
            }
        }
        /// <summary>
        /// binding major node
        /// </summary>
        private void BindMajorNode()
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                IQueryable<family> persons = dc.family.Where(u => u.parentId == null);//get the major persons
                foreach (family person in persons)//traversal major persons
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = person.name;
                    tn.Value = person.id.ToString();
                    BindChlidNode(tn, person.id);//binding child node
                    TreeView1.Nodes.Add(tn);
                }
            }
        }
        /// <summary>
        /// binding child node 
        /// </summary>
        /// <param name="tn">parent node</param>
        /// <param name="parentId">parent id</param>
        private void BindChlidNode(TreeNode parentNode, int parentId)
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                IQueryable<family> persons = dc.family.Where(u => u.parentId == parentId);//get child persons
                foreach (family person in persons)//traversal child persons
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = person.name;
                    tn.Value = person.id.ToString();
                    BindChlidNode(tn, person.id);//recursive 
                    parentNode.ChildNodes.Add(tn);
                }
            }
        }
    }
}

좋은 웹페이지 즐겨찾기