views:

489

answers:

2

I have a UserControl that provides some functionality (loads and displays a client's information) and I want to extend that UserControl. Some forms require the plain old UserControl while other forms will require some additional fields. How can I inherit from the existing UserControl, maintain the existing fields but add new fields on as well?

I like the way MasterPages work, where you define place holders, and then the content pages just fill those places holders with extra content. Is this possible with UserControls? Thanks!

A: 

Perhaps you could define a few ASP:PlaceHolders (think of it as "zones") within your UserControl and make them available via public (or protected if you only want the inherited class to be able to change them) properties. That way you can add subcontrols to them whenever you need to.

protected PlaceHolder TopZone {
   get { return plhTop; }
}


protected PlaceHolder BottomZone {
   get { return plhBottom; }
}

From one of the inherited UserControls:

Label lblTest = new Label { Text = "Hello World!" };
TopZone.Controls.Add(lblTest);

It's a verrry basic example, I hope it is of use to you.

Zyphrax
This is quite similar to the method I am using right now, but it doesn't provide the same level of functionality that MasterPages provides, and it's been difficult to maintain. :(
Jon Tackabury
The UserControl is a control (container) itself you could simply add controls to it. But perhaps now you're losing the point a bit of using a UserControl.
Zyphrax
A: 

I ended using nested MasterPages as my UserControls (as they inherit from UserControl anyway). Then I just call this.LoadControl("~/mp.master") and voila, I have nested UserControls with full design-time support. :)

Jon Tackabury