@Juraj - I've developed Portal engine sites using C# code and the Kentico API in ascx layouts extensively, and I've never experienced that error. (Kentico 10 and beyond)
Also, the documentation says this:
ASCX layout type - This type of layout code supports both HTML and ASP.NET markup, i.e. the same syntax that you would use to edit a standard web form or user control, including inline code and embedded controls.