UserControl question

  • Thread starter Thread starter Shawn
  • Start date Start date
S

Shawn

Hi.
When I create a new UserControl in Visual Studio it automatically declares
it as "MustInherit". In order for me to call a method in my UserControl I
have to remove the "MustInherit" part. Am I facing any "consequences" by
removing it? Are you not suppose to be able to call a method in a
Usercontrol?

Thanks,
Shawn
 
The code behind on a user control is abstract - the user control itself is
not abstract. So, if you include a user control on your page, the
dynamically compiled class inherits from your code behind, and is
instantiable. That is why you have to use Page.LoadControl in order to
dynamically load a control, rather than just instantiate a new instance of
that class and add it in.

So, you just need to work with an instance of a derived class. If you are
working with a user control that you created declaratively, just use a
reference to that control in your code behind. Make sure the control has an
ID, that an instance of that is defined in your code behind (which will be a
base class reference), and then call methods using that reference. If you
are creating user controls dynamically, just hold on to the control variable
returned by LoadControl.
 
Back
Top