Define a property of a base class (ascx) in a custom UserControl, and instanciate a child class in the aspx
I defined 3 custom usercontrols like that :
public partial abstract class MyAbstractControl : Usercontrol{
// Base class definition here, with common property and methods
public string CommonAttribute {get; set;}
}
public partial class MyConcreteControl1 : MyAbstractControl{
// Some specific stuff here
}
public partial class MyConcreteControl2 : MyAbstractControl{
// Other specific but different stuff here
}
Then I defined another UserControl that have a property of the base class :
public partial class MyBeautifulControl : UserControl{
[PersistenceMode(PersistenceMode.InnerProperty)]
public MyAbstractControl ChildElement{get;set;}
}
In the aspx file, I'm using this control, but I'd like to define an instance of MyConcreteControl1 instead of MyAbstractControl
But If I write :
<MyBeautifulControl ru开发者_开发问答nat="server" id="beautiful">
<ChildElement commonAttribute="value" />
</MyBeautifulControl>
The ChildElement can only be defined as a MyAbstractControl instance. I'd like to create a MyConcreteControl1 or an MyConcreteControl2 instance, depending on the context, and I don't know how.
Use the System.Web.UI.TemplateContainerAttribute
:
[PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(MyConcreteControl))]
public MyAbstractControl ChildElement{get;set;}
see the documentation for details. There is also a HOWTO about.
精彩评论