ASP.NET Custom Control With A Property That Is An Interface
I want to have a property on my custom control that is an interface type. For example:
[
ToolboxData("<{0}:MyTextBox runat=server></{0}:MyTextBox"),
ParseChildren(true, "Validation")
]
class MyTextBox : WebControl
{
[
Category("Behavior"),
Description("The validation to use"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerDefaultProperty)
]
public IValidation Validation { get开发者_StackOverflow社区; set; }
}
Then when I go to use my control in a web form I would like to be able to do:
<my:MyTextBox ID="txt" runat="server">
<my:FancyValidator />
</my:MyTextBox>
That way I will be able to define one class that could use any number of validators. When I try and do this now, I end up with an error saying:
Type 'IValidator' does not have a public property named 'FancyValidator'
What do I need to do to make this work?
Thanks!
Honestly, the easiest thing to do would be to set the property in the code-behind instead of in the markup.
As a side note, I found that I can make this work if I make Validation of type List then the markup looks like this:
<my:MyTextBox ID="txt" runat="server">
<Validation>
<my:FancyValidator />
</Validation>
</my:MyTextBox>
Not quite what I was after, but works.
精彩评论