I'm building a custom web part with a custom toolpart to edit its custom properties.
I'm wondering where should I valide the values.
I can see two places where I can validate.
Either in the property itself:
private int? m_MaxNumberOfSomething;
[Browsable(false)]
[Category(Constants.WebPartsExtendedPropertiesCategoryName)]
[DefaultValue(5)]
[WebPartStorage(Storage.Personal)]
public int? MaxNumberOfSomething
{
get
{
return m_MaxNumberOfSomething;
}
set
{
if (value.HasValue && value < 1) throw new WebPartPageUserException("At least one !");
m_MaxNumberOfSomething = value;
}
}
Or in the apply changes of the toolpart method :
public override void ApplyChanges()
{
int maxNumberOfSomething;
if (!int.TryParse(txtMaxNumberOfSomething.Text, out maxNumberOfSomething))
{
throw new WebPartPageUserException("At least one");
}
ParentWebPart.MaxNumberOfSomething = maxNumberOfSomething;
}
Is there any 3rd way?
What is the correct place to add such validation?