BaseFieldControl.ErrorMessage Property
Gets or sets a message indicating that, and possibly why, the current value of Value is invalid.
Namespace: Microsoft.SharePoint.WebControls
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
Syntax
'Declaration
Public Property ErrorMessage As String
Get
Set
'Usage
Dim instance As BaseFieldControl
Dim value As String
value = instance.ErrorMessage
instance.ErrorMessage = value
public string ErrorMessage { get; set; }
Property Value
Type: System.String
A String representing the error message.
Implements
Examples
The following is an example of an override of the Validate method that sets ErrorMessage. It first checks to see if the current control mode is Display, in which case it does not matter if the field is invalid because it cannot be changed anyway. The method also looks to see if the Value property is already known to be invalid by checking the IsValid property. If either of those checks is true, it does nothing. If neither is true, the method calls the base Validate which will set IsValid to false if it finds anything wrong with the Value property and set an appropriate ErrorMessage. (The BaseFieldControl.Validate method does nothing, so if the class whose Validate method is being overridden is derived directly from BaseFieldControl, then the call to the base Validate method can be omitted.) Finally, the method checks the value of Required and enforces that value.
public override void Validate()
{
if (ControlMode == SPControlMode.Display || !IsValid)
{
return;
}
base.Validate();
if (Field.Required &&
(Value == null || Value.ToString().Length == 0))
{
this.ErrorMessage = Field.Title + " must have a value."
IsValid = false;
return;
}
}
Public Overrides Sub Validate()
If ControlMode = SPControlMode.Display OrElse (Not IsValid) Then
Return
End If
MyBase.Validate()
If Field.Required AndAlso (Value Is Nothing OrElse Value.ToString().Length = 0) Then
Me.ErrorMessage = Field.Title & " must have a value."
IsValid = False
Return
End If
End Sub