protected void Page_Load(object sender, EventArgs e)
 {
     if (!IsPostBack)
     {
         MenuNode root = ConvertTableToTree(GetTreeTable());
         foreach (MenuNode topLevelNode in root.Children)
         {
             MyMenu.Items.Add(topLevelNode.ToMenuItem());                     // Visits all nodes in the tree.
         }
     }
 }
        // See e.g. http://stackoverflow.com/questions/2654627/most-efficient-way-of-creating-tree-from-adjacency-list
        // Assuming table is ordered.
        static MenuNode ConvertTableToTree(DataTable table)
        {
            var map = new Dictionary <int, MenuNode>();

            map[0] = new MenuNode()
            {
                Id = 0
            };                                              // root node

            foreach (DataRow row in table.Rows)
            {
                int nodeId   = int.Parse(row["Id"].ToString());
                int parentId = int.Parse(row["ParentId"].ToString());

                MenuNode newNode = MenuNodeFromDataRow(row);

                map[parentId].Children.Add(newNode);
                map[nodeId] = newNode;
            }

            return(map[0]);            // root node
        }