I have a ASP.Net project that is setup in such a way that it can be dropped into any site and “just work.” All the paths are relative to the current file, not relative to the “~”. The paths are determined by ThePath = this.TemplateSourceDirectory;
This is working for everything expect registering a custom control that is created and added to one of the pages. I can add the control just fine with the Page.LoadControl but I cant cast it as the correct type to access anything.
How can I add a reference to the class from within the code itself?
If you don’t know the control’s specific type ahead of time, this isn’t possible. The class must derive from
UserControl, so you can cast it to aUserControland you’ll have access to all the methods and properties on that class. If there’s some special information or functionality you need to require all controls to have, and you need to be able to assume those are always present, then you will have to write your own class that derives fromUserControl, and require all custom controls to derive from that instead:Then you could cast all controls at load-time to this
SpecialControlBase, and have access to theDoSomethingSpecialmethod.But as far as the most-specific members of a class loaded at runtime, think about it – if I write my own control called
RexsUserControland drop it into your application, there’s no way you could know what methods and fields I’ve put on my control, so you can’t write any code that references those members specifically.