是否可以初始化控件的列表标记中的属性?
假设我们有以下内容:
public enum RenderBehaviors
{
A,
B,
C,
}
public class MyControl : Control
{
public List<RenderBehaviors> Behaviors { get; set; }
protected override void Render(HtmlTextWriter writer)
{
// output different markup based on behaviors that are set
}
}
是否可以在 ASPX/ASCX 标记中初始化 Behaviours 属性? 即:
<ns:MyControl runat="server" ID="ctl1" Behaviors="A,B,C" />
在这种情况下,子类化不是一个选项(行为的实际意图与此示例略有不同)。 当我尝试以这种方式初始化属性时,WebForms 会生成解析器错误。 同样的问题也适用于其他列表类型(int、字符串)。
Let's say we have the following:
public enum RenderBehaviors
{
A,
B,
C,
}
public class MyControl : Control
{
public List<RenderBehaviors> Behaviors { get; set; }
protected override void Render(HtmlTextWriter writer)
{
// output different markup based on behaviors that are set
}
}
Is it possible to initialize the Behaviors property in the ASPX/ASCX markup? i.e.:
<ns:MyControl runat="server" ID="ctl1" Behaviors="A,B,C" />
Subclassing is not an option in this case (the actual intent of the Behaviors is slightly different than this example). WebForms generates a parser error when I try to initialize the property in this way. The same question could be applied to other List types (int, strings).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过进一步研究,我发现 WebForms 确实使用了 TypeConverter(如果它能找到的话)。 类型或属性需要正确修饰,详见 相关问题。
我最终实现了类似的东西:
After researching this further, I found that WebForms does use a TypeConverter if it can find it. The type or the property needs to be decorated properly, as detailed in this related question.
I wound up implementing something similar to this:
使用标记时,您自己对字符串属性的建议是唯一的解决方案。
Your own suggestion of a string property is the only solution when working with markup.