Asp.net - grid validation - repeating validation messages

I have a gridview that contains from one to many rows (like most of them) - each with a text input field. Each line has a required ID for this text box. When the form is submitted, the gridview is validated, it is quite possible that more than one row has an empty text box. This leads to repetition of verification messages, for example.

  • Enter the text for the "Name" field
  • Enter the text for the "Name" field
  • Enter the text for the "Name" field

Can these messages be combined into one message?

I know it is possible to create a validator by customizing the validator class and inheriting from the BaseValidator class which can be used to validate the gridview as a whole. But I put an image against each line when it is invalid, so I have to imagine that separate validators are required for each line.

+1


source to share


2 answers


This is a solution that uses CustomValidator and requires several organizational changes. This requires a postback because authentication with the CustomValidator is done on the server side.

Here's the setting:

  • For each of your existing RequiredFieldValidators that display the message "Please provide text for the" Name "field, you need to set:
    •   
    • EnableClientScript = "false"  
    • ValidationGroup = "vgTxtName" (enter your name)  
    • ErrorMessage = "" (or remove it altogether, CustomValidator will be responsible for this)
    You have the option to display nothing (less understandable to the user) or display an asterisk to indicate which validator is invalid.
    Option 1:
    • Display = "None"
    Option 2 (preferred):
    • Display = "Dynamic"
    • Set text between validation tags: *
  • No changes required to your ValidationSummary control (it must be neutral and not have the ValidationGroup attribute set, which is the default)
  • Add CustomValidator (see code below)
    •   
    • Add an event handler for the CustomValidator ServerValidate event (you can just double click on the design to create it)  
    • Implement event handler logic (see code below)

The idea is to not allow the page to handle these RequiredFieldValidators anymore, and instead we will allow the CustomValidator to do so.

An example of a TextBox RequiredFieldValidator (you should have something similar to this with the appropriate id names that match step 1 above):

Option 1:

<asp:RequiredFieldValidator ControlToValidate="txt1" ID="rfv1" runat="server" 
EnableClientScript="false" Display="None" ValidationGroup="vgTxtName" />

      

Option 2:



<asp:RequiredFieldValidator ControlToValidate="txt1" ID="rfv1" runat="server" 
EnableClientScript="false" Display="Dynamic" ValidationGroup="vgTxtName">*
</asp:RequiredFieldValidator>

      

CustomValidator markup (you can place this everywhere intelligently, for example next to the ValidationSummary element):

<asp:CustomValidator ID="cvName" runat="server" Display="None"
ErrorMessage="Please provide text for 'Name' field" 
OnServerValidate="cvName_ServerValidate" />

      

Here comes an error message that replaces the units from the individual validators. Also note the lack of a ControlToValidate set that is valid for this type of validator and is useful for applying validation that spans multiple controls.

CustomValidator EventHandler (cvName_ServerValidate):

protected void cvName_ServerValidate(object source, ServerValidateEventArgs args)
{
    // Validate vgTxtName group
    Page.Validate("vgTxtName");

    // .NET 3.5 - add using System.Linq;
    args.IsValid = Page.GetValidators("vgTxtName")
                        .OfType<RequiredFieldValidator>()
                        .All(v => v.IsValid);

    // .NET 2.0 (use either this or the above, not both)
    bool isValid = true;
    foreach (RequiredFieldValidator validator in Page.GetValidators("vgTxtName"))
    {
        isValid &= validator.IsValid;
    }
    args.IsValid = isValid;
}

      

What is it! Just keep in mind that this is strictly for RequiredFieldValidators. You should not place different types of validators in the "vgTxtName" group, as the cvName logic is strictly related to the RequiredFieldValidator type. You will need to tweak different groups or tweak your code if you are going to use other types of validators.

+2


source


I would suggest not using the Validator summary.

Change the Text property or internal content of the validators to something more appropriate for your application.

For example...

<asp:Validator ID="X" ... runAt="server" Text="*" />

      

or



<asp:Validator ID="X" ... runAt="server">*</asp:Validator>

      

or to display an image ...

<asp:Validator ID="X" ... runAt="server"><img src="../path.png" alt="Invalid" /></asp:Validator>

      

I also use a validator to change the pointer to the help cursor and add a ToolTip property to display the same error message.

0


source







All Articles