在设计视图中必需标志控制属性 [英] Flag control properties as required in design view
问题描述
我想强制控制的消费者给予的属性放置在页面上控制时的值。
I'd like to force the consumer of a control to give a property a value when placing the control on a page.
在VisualStudio中,当你创建一个<而对用户控件属性SRC或ALT img>标签时,它会强调说,SRC和ALT需要的属性。我以为这只是在编辑器中的标签的特殊处理,但有没有办法来定义控制的类似行为?
In VisualStudio when you create an < img > tag without attributes SRC or ALT on a user control, it gets underlined saying that SRC and ALT are required attributes. I assume this is just a special handling of the tag by the editor, but is there a way to define a similar behavior for controls?
如果控制过这样定义的属性:
If the control had a property defined like this:
public object AProperty
{
get
{
if (ViewState["AProperty"] == null)
{
throw new Exception("AProperty is a required property of this control");
}
return ViewState["AProperty"];
}
set { ViewState["AProperty"] = value; }
}
有没有办法使用自定义属性或别的东西,一个办法是标志设计?
Is there a way to use a Custom Attribute or something else that would flag in the designer?
推荐答案
您可以使用Microsoft.Build.Framework.Required属性。这需要一个值在构建时要设定或构建将失败,表明该属性没有一个值的消息。
You could use the Microsoft.Build.Framework.Required attribute. This would require a value to be set at build time or the build will fail with a message which indicates that the property does not have a value.
我不相信这是表示一个特定的标签必须包含在一个服务器控件(或至少我没有看到对System.Web.UI.HtmlControl.Image类中的任何这样的属性)的属性。我认为,痘痘下划线是IDE的HTML验证的一部分。
I don't believe there is an attribute to indicate that a specific tag must be included in a server control (or at least I don't see any such attribute on the System.Web.UI.HtmlControl.Image class). I believe that the litle underlines are part of the HTML validation of the IDE.
您可以随时创建,如果属性缺失会抛出一个警告的自定义属性
You could always create a custom attribute which throws a warning if a property is missing
这篇关于在设计视图中必需标志控制属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!