views:

1647

answers:

3

In Windows Forms it's easy to inherit one user control from another. The inherited control contains all the controls of the parent form, and you can add some more. Can the same be done with ASP.NET? I'm talking about the .ASCX type of user controls.

+3  A: 

The difference between the windows forms example and the web forms example, is the control has a tagged portion. I imagine this can be overcome, some day, as web applications compile everything (at least in some instances), but it is not the case today.

You can, however, slap a user control on another user control, ad nauseum. It will not give you inheritance, but it does allow you to adorn a particular control with additional controls by making a composite of the original control and the stuff you need to add to it. That might solve your problem?

Gregory A Beamer
It might. I was just wondering if there was a more elegan way. :)
Vilx-
I understand. Unfortunately, I don't think there is today, due to the separation of tags and code.
Gregory A Beamer
"You can, however, slap a user control on another user control, ad nauseum". YES YES YES. Completely didn't think of nesting UserControls
Greg B
+1  A: 

You could certainly inherit the base user control in code and insert your additional controls in code, if you are talking building in a visual designer then I'm not sure. I'm aware of creating composite controls from both system.web controls and user controls so there's no reason that your new control couldn't contain another user control with additional controls added around it.

Lazarus
+3  A: 

The short answer is no it can't be easily done. There's no inheritance between user controls or webforms. You can create an inheritance hierarchy between the code behinds classes but the gui components aren't inherited.

Tundey