I'm creating a Page Dynamically in ASP.NET and i want to load a Control inside the form but everytime I try to Add it to Page.Form.Controls the Form is null and gives out a NullReference
Page myPage = new Page();
FormAtt ctrl = (FormAtt)myPage.LoadControl("path/to/my/file.ascx");
myPage.Form.Controls.Add(ctrl);
I'm trying to do this because I have to parse the controls HTML , while firing the events and the logic inside the control, into a string to fill a (really bad designed) String with HTML code, if I just call ctrl.RenderControl(HtmlWriter)
the control will not render and it appears that the Events are not fired, i guessed i had to have a page to make the the event flow work, but I really didn't want to create a new aspx just for this.
Also, I'm sure the path is correct because I tested the same loadcontrol and becaus it doesn't raise an exception as it raises when I change the path to something non-existant. I can't redesign the code that assembles the HTML because it's just too much work and It's out of the scope of what i'm doing, the scope is not decided by me.