Personal notes on software development.
For Java technologies check my dedicated site

Pages

By default, the validation controls automatically emit client-side validation script; they also provide server-side validation on postback, storing the cumulative result in the Page.IsValid property.

ASP.NET provides five built-in validation controls that are designed to be used to validate the value of a single input control [1]:

For more information on these five controls, check out the Validation Controls section of the ASP.NET Quickstart Tutorials.

Some control notes:

RequiredFieldValidator important properties:

  • ControlToValidate: All validation controls work by validating the input of a single ASP.NET Web control. Therefore, we need to indicate that the (for ex.) RequiredFieldValidator should validate against some TextBox in the page;
  • ErrorMessage: for ex. "You must provide the product's name". This is used by the ValidationSummary control;
  • Text: for ex. "*". This is the text that is displayed by the validation control if the validation fails (if this property value is omitted, the ErrorMessage property value is also the text displayed by the validation control on invalid input.);
  • ValidationGroup: associate this validation control with a set of validation controls;

CompareValidator important properties notes:

  • ControlToValidate: All validation controls work by validating the input of a single ASP.NET Web control. Therefore, we need to indicate that the (for ex.) RequiredFieldValidator should validate against some TextBox in the page;
  • ErrorMessage: for ex. "You must provide the product's name". This is used by the ValidationSummary control;
  • Text: for ex. "*". This is the text that is displayed by the validation control if the validation fails (if this property value is omitted, the ErrorMessage property value is also the text displayed by the validation control on invalid input.)
  • Operator: for ex. "GreaterThanEqual";
  • ValueToCompare: for ex. "0";
  • Type: for ex. "Currency";
  • ValidationGroup: associate this validation control with a set of validation controls;

Example:
<InsertItemTemplate>
    <asp:TextBox ID="InsertUnitPrice" runat="server"
Text='<%# Bind("UnitPrice") %>'></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
        ControlToValidate="InsertUnitPrice" 
        ErrorMessage="O preço é de preenchimento obrigatorio."
        ValidationGroup="InsertValidationControls">*</asp:RequiredFieldValidator>
    <asp:CompareValidator ID="CompareValidator2" runat="server" 
        ControlToValidate="InsertUnitPrice" 
        ErrorMessage="O preoço deverá ser superior a 0€" Operator="GreaterThan" 
        ValueToCompare="0"
        ValidationGroup="InsertValidationControls">*</asp:CompareValidator>
</InsertItemTemplate> 


ValidationSummary control:

This control displays the ErrorMessages of those validation controls in the page that detected invalid data. This summary data can be displayed as text on the web page or through a modal, client-side messagebox.

Relevant properties:
  • ShowSummary: (true/false) if the summary should be rendered on the page;
  • ShowMessageBox: (true/false) if the summary should be displayed as a messagebox (modal client-side popup); In this case the location of the Validation control on the page doesn't really matter, since it will display as a messagebox.
  • ValidationGroup: associate this validation control with a set of validation controls;

Other Notes:

  • ValidationGroup notes: The Button and Button-related controls in ASP.NET also include a ValidationGroup property. A validation group's validators are checked for validity only when a postback is induced by a Button that has the same ValidationGroup property setting. For example, in order for a DetailsView's Insert button to trigger a "someGroup" validation group we need to set the CommandField's ValidationGroup property to"someGroup". 
  • All of the ASP.NET validation controls repeat their validation logic immediately upon postback (on the server) and report the overall validity of the page inputs via the Page.IsValid property.
    If a user has JavaScript disabled  the client-side validation will be bypassed and a postback will ensue either the input data is valid or not. To prevent this situations we can check the validation on the server and if there are errors the server will respond with a new page with the Validation Controls rendered. To perform this we only need to check if the Page.IsValid property is true before executing the server code request.
    Some Controls already perform this check on server for us, like the GridView. [2]
    Example:


     protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            if (!Page.IsValid)
                return;
           (proceed execution only if the inputs are valid)
        }

Related articles:
[1] - Adding Validation Controls to the Editing and Inserting Interfaces
[2] - Adding Validation Controls to the DataList's Editing Interface

No comments:

Post a Comment