问题
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).
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/1106134/how-do-i-specify-a-required-attribute-in-a-custom-net-web-control