views:

159

answers:

1

I'd like to force the consumer of a control to give a property a value when placing the control on a page.

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?

+1  A: 

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.

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

jellomonkey
Yea that's what I figured. Not a necessity, but it would have been nice. I think I'll add a ToolboxData attribute with the Property in it, so it's there right away.
s_hewitt