I just discovered a little tip with dynamic control creation via the ASP.NET LoadControl() method that might be helpful.
LoadControl() has two signatures:
- LoadControl(System.Type, object)
I was trying to use the 2nd version and pass along a parameter object to the constructor of my user control. This user control contained a FormView. Everything compiled, but I kept getting a runtime NullReferenceException telling me that my FormView was NULL. Odd, since I basically cut and pasted the control from a different control that was known to work. The only difference was that this time I was dynamically loading the control.
I switched to using the first version of LoadControl, passing in a virtual path to the control, and everything worked fine.
I can’t say exactly why, but something about the Render chain is different between the two versions.