Question
How can I use a .NET CustomValidator control with server-side validation?
Background
Validation using .NET CustomValidator controls cannot be executed client-side using JavaScript because it involves calling methods or other objects only available server side (code-behind).
If you want to use a .NET CustomValidator control with client-side validation code, see How can I use a .NET CustomValidator control in my WorkflowGen web form?.
Notes
-
In order to apply your custom validation to the
WFGENPagevalidation group, you should have at least one field specified in theFORM_FIELD_REQUIREDparameter (see My .NET validator does not trigger the validation error message), otherwiseWFGENPagewill not be instantiated and your validation result will not affect your WorkflowGen page submission. -
When using a .NET CustomValidator control and validation that occurs server-side, the error cannot be combined with the
WFGENPagevalidation groups submission validator report dialog box since WorkflowGen handles its default validation at client-side before the form is posted back to the server. You will need to either display the error message at the position of your CustomValidator control on your webform (set your CustomValidatorDisplayattribute toDynamic) or manually trigger a JavaScript alert dialog box when the page is reloaded. -
The
ControlToValidateattribute may not be necessary. You can specify which fields you would like to validate in your server-side validator method and define the final validation resultargs.IsValidto either be true or false. If you do specify a value for theControlToValidateattribute with a control ID (normally applied to controls such as a TextBox, DropDownList, etc., common text-based .NET controls) you can perform the validation by checkingargs.Valueinstead of using the control ID in your server-side validator method.
Examples
Web form:
<asp:TextBox runat="server"></asp:TextBox>
ControlToValidate="REQ_ AMOUNT"
Display="Dynamic"
ErrorMessage="The amount is invalid"
OnServerValidate="MyRangeValidator"
ValidateEmptyText="True"
ValidationGroup="WFGENPage"
EnableClientScript="False"
SetFocusOnError="True">
</asp:CustomValidator>
In code-behind (C#):
protected void MyRangeValidator(object source, ServerValidateEventArgs args)
{
if (args.Value != string.Empty
args.Value matches your validation criteria)
{ args.IsValid = false; }
else
{ args.IsValid = false; }
}
Test environment:
- The EFORMASPX application parameter
FORM_FIELD_REQUIREDmust have at least one control ID specified. You can simply specify the field to be validated. In the above example, we useREQ_AMOUNT.
Test pattern:
-
If you click Submit without entering a value for
REQ_AMOUNT, the web form will perform a post back and return with the error message next to theREQ_AMOUNTtextbox. -
If you click Submit with a valid value entered in
REQ_AMOUNT, the web form will submit back to WorkflowGen.