Hi,
it is the difference between code-behind and inline code and its impact what
you see here. Code-behind model in v1 and v1.1 involves two classes, the one
that is compiled dynamically (by page parser) and then the actual
code-behind class (which you develop with VS.NET). The dynamically created
class inherits from your code-behind class, that's the idea briefly.
Now when you instantiate the control like: Dim mC As New MyControl, you
instantiate the code-behind class but you don't cause parsing happen for the
ascx (which contains the markup). E.g you create control instance of the
code-behind class but not the dynamic class (which is created when ascx is
parsed) and the code-behind class lacks the markup which is in dynamic
class.
When you create the control instance with LoadControl, ascx is parsed and
the ascx file contains details about the relevant code-behind class (in
directive) and therefore the correct code-behind class is instantiated also.
--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
"Brandon Potter" <msnews@brandonpotter_nospam.com> wrote in message
news:uC**************@TK2MSFTNGP10.phx.gbl...
Why is it that when I override the OnInit method for a page, I can add a
user control like this:
Me.Controls.Add(LoadControl("MyControl.ascx"))
(where MyControl.ascx is your every day user control)
... but when I use:
Dim mC As New MyControl
Me.Controls.Add(mC)
... it doesn't show up?
It works if use the generic Control type:
Dim mC As Control
mC = LoadControl("MyControl.ascx")
Me.Controls.Add(mC)
I thought ASP.NET would add user controls to a page just the same as
controls... am I missing something?
Brandon