BaseFieldControl.Validate 方法
当在某个派生类中重写,验证Value的值满足所有限制字段内容 (如长度、 格式和数据类型。
命名空间: Microsoft.SharePoint.WebControls
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
Public Overridable Sub Validate
用法
Dim instance As BaseFieldControl
instance.Validate()
public virtual void Validate()
实现
备注
Validate没有默认实现。
针对继承者的注释
You should consider implementing this method in your derived classes. It could be used, for example, check Required property and verify that the field has a value whenever Required is true. See also the CreateChildControls property and Custom Field Data Validation for more about validation.
Value具有BaseFieldControl作为其FieldRenderingControl属性的SPField对象是在 UI 中,不是基础值 (即ItemFieldValue) 控件的值。仅当您需要验证的用户界面值时,请使用Validate 。若要验证ItemFieldValue,请使用GetValidatedString()。
Validate的替代应IsValid属性设置为false ,并将ErrorMessage设置为相应的消息。
示例
下面是Validate方法的重写的示例。首先检查以查看当前控制模式是否显示,这种情况下它并不重要是否域无效,因为它仍然不能更改。此方法还查看如果已经已知Value属性可通过检查IsValid属性无效。如果任一这些检查,则返回 true,则没有任何影响。如果既不为 true,该方法将调用基Validate会将IsValid设置为false ,如果找到Value属性有什么问题,并设置相应的ErrorMessage。( BaseFieldControl.Validate方法不起作用,如果直接从BaseFieldControl派生其Validate方法将被覆盖的类,因为对基Validate方法的调用可以省略。)最后,该方法检查Required的值,并强制该值。
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
另请参阅
引用
Microsoft.SharePoint.WebControls 命名空间
其他资源
Patterns of Custom Field Rendering