views:

236

answers:

3
private string _itemId;

[Browsable(true),
Description("Required identifier for the Item.")]
public string ItemId
{
    get { return _itemId; }
    set
    {
     if (string.IsNullOrEmpty(_itemId))
     {
      _itemId = value;
     }
    }
}

How would I actually make that required when someone uses the control? I'm trying to find an attribute that says something like Required(true).

A: 

I don't know that there's an attribute for this. I believe on the Page_Load event (or perhaps some rendering event) just check if the value has been set. If not then throw an exception.

Spencer Ruport
A: 

I don't think this is possible. Consider that the designer needs to be able to create an instance of the control when it's dragged from the toolbox. At that time, it's going to have default values for properties, and these values need to be valid.

John Saunders
A: 

You can use the System.ComponentModel.DataAnnotations namespace and the RequiredAttribute class within it.

More info can be found here: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute%28v=VS.95%29.aspx

cougardent