Asp.net validation error message to change labels

2019-07-13 04:46发布

问题:

I am using asp.net validation controls for validating user input. What i want is to change the label text with the error message generated by the validation control.

<asp:Label ID="Label1" runat="server" Text="Whats your name"></asp:Label>
<asp:TextBox ID="nameB" Width="322px" Height="30px" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="business" runat="server" ErrorMessage="Please tell us your name." ControlToValidate="nameBuisness" CssClass="errMsg" Display="Dynamic"></asp:RequiredFieldValidator>

Thank you.

回答1:

One way is to handle the submit-button's OnClientClick-event and call a javascript function like:

<script type="text/javascript">
    function displayValidationResult() {
        // Do nothing if client validation is not available
        if (typeof (Page_Validators) == "undefined") return;
        var LblName = document.getElementById('LblName');
        var RequiredName = document.getElementById('RequiredName');
        ValidatorValidate(RequiredName);
        if (!RequiredName.isvalid) {
            LblName.innerText = RequiredName.errormessage;
        }
    }
</script>

<asp:Label ID="LblName" runat="server" Text="Whats your name"></asp:Label>
<asp:TextBox ID="TxtNameBusiness" Width="322px" Height="30px" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredName" ValidationGroup="business"
    runat="server" ErrorMessage="Please tell us your name." ControlToValidate="TxtNameBusiness"
    CssClass="errMsg" Display="None"></asp:RequiredFieldValidator>
<asp:Button ID="BtnSumbit" runat="server"  Text="Submit"
    OnClientClick="displayValidationResult()" ValidationGroup="business" />

I've used some of the few ASP.NET client validation methods available. I've also renamed your controls to somewhat more meaningful and added a submit-button.

ASP.NET Validation in Depth



回答2:

If your requirement is that you want to do this validation using the built-in ASP.Net validation controls, then you will need to use the ASP.Net CustomValidator control. This cannot be done using the ASP.Net RequiredFieldValidator control. To do the validation you specified, put a CustomValidator control on on your page so that the markup looks like this:

<asp:Label ID="Label1" runat="server" Text="Whats your name"></asp:Label>

<asp:TextBox ID="nameB" Width="322px" Height="30px" runat="server"></asp:TextBox>

<asp:CustomValidator ID="CustValidator" runat="server" ControlToValidate="nameB" 
    ValidateEmptyText="true"  ErrorMessage="Please tell us your name."  
    onservervalidate="CustValidator_ServerValidate" Display="none"  >**</asp:CustomValidator>

<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click"  CausesValidation="true"  />

You then need to write your custom validator. The server-side validation code looks like this:

    protected void CustValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (string.IsNullOrWhiteSpace(args.Value))
        {
            Label1.Text = CustValidator.ErrorMessage;
            args.IsValid = false;
        }
        else
        {
            Label1.Text = "Whats your name";
            args.IsValid = true;
        }
    }

This custom validator will give you the behavior you desire.

In general when you use a CustomValidator control, you should do both server-side validation (for security) and client-side validation (for a better UI experience). To get client-side validation, you will need to write a client-side function in javascript and/or jQuery to do similar validation and then assign it to the ClientValidationFunction of the custom validator.

Further information on the CustomValidator control can be found in the MSDN documentation.