The reason there is a difference is because in the second instance you are bypassing the binding mechanism of the the two partial classes.
When you include the ascx control in the page as a normal declaration, the controls declared within it (the label in your example) are automatically instantiated by the framework as the class is instantiated.
This is a binding mechnism that reads the "front-end" ascx file and instantiates objects in the class.
When you use LoadControl(string) - you are doing exactly the same thing.. it uses a virtual path to get the "front-end" file and ensure controls within it are instantiated.
However, when you use the new (to 2.0) LoadControl(type, object) version, there is no virtual path available and it rightly so makes no assumptions (as it cannot ensure that a type has a front end file).
The framework cannot instantiate any controls within the ascx page and they are therefore null.
This is why you receive an error stating that your lblInfos object is a null reference.
Partager