How do I specify a required attribute in a custom .NET Web control?

陌路散爱 提交于 2019-12-22 04:20:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!