在自定义 UserControl 中定义基类 (ascx) 的属性,并在 aspx 中实例化子类
我定义了 3 个这样的自定义用户控件:
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
}
然后我定义了另一个具有基类属性的 UserControl:
public partial class MyBeautifulControl : UserControl{
[PersistenceMode(PersistenceMode.InnerProperty)]
public MyAbstractControl ChildElement{get;set;}
}
在 aspx 文件中,我正在使用此控件,但我想定义 MyConcreteControl1 的实例strong> 而不是 MyAbstractControl
但如果我写:
<MyBeautifulControl runat="server" id="beautiful">
<ChildElement commonAttribute="value" />
</MyBeautifulControl>
ChildElement 只能定义为 MyAbstractControl 实例。我想根据上下文创建一个 MyConcreteControl1 或 MyConcreteControl2 实例,但我不知道如何操作。
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 runat="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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
System.Web.UI.TemplateContainerAttribute
:请参阅 文档了解详细信息。还有一个HOWTO。
Use the
System.Web.UI.TemplateContainerAttribute
:see the documentation for details. There is also a HOWTO about.