Problema con el validador de campo obligatorio de Asp.net

Estoy utilizando asp.net validations y jquery validation for checking user input.I am using number of required field validators y custom validators in my form , i am facing a strange problem , when a field with required validator is empty , the error message se muestra en validation summary. this is all right.When the same field is left empty and wrong inputs are entered on other fields with custom validators ,, the validation summary only displays the error message of required field validator.The other error messages are just not displayed.Can anybody point me out where am i going wrong. Thanks

preguntado el 10 de marzo de 12 a las 06:03

I think we'll need to look at your code. -

1 Respuestas

Aquí hay una muestra de código que usa Validador de campo requerido Junto con Validador personalizado. It validates both fields, as well as when one of them fails, and displays the error messages in the Validación Resumen.

El Validador personalizado has OnServerValidate function in the code behind.

The .aspx markup:

            <form id="form1" runat="server">
                <div>
                    <label>Name:</label>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" EnableClientScript="false"
                    ControlToValidate="TextBox1" ErrorMessage="The 'Name' field cannot be empty!" Text="*" ForeColor="Red" 
                    Display="Dynamic"></asp:RequiredFieldValidator>

                    <br /><br />

                    <label>Number:</label>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    <asp:CustomValidator ID="CustomValidator1" runat="server" ValidateEmptyText="true"
                    ControlToValidate="TextBox2" ErrorMessage="The 'Number' field must be exactly 5 digits!" Text="*" ForeColor="Red" 
                    Display="Dynamic" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>

                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Please check the following fields:" ForeColor="Red" DisplayMode="BulletList" />

                    <br />

                    <asp:Button ID="Button1" runat="server" Text="Submit" />
                </div>
            </form>

and the validation function in .aspx.cs file:

            protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
            {
                if (args.Value.Length < 5 || args.Value.Length > 5)
                {
                    args.IsValid = false;
                }
                else
                {
                    args.IsValid = true;
                }
            }

In order to be able to display both error messages, and not only the Validador de campo requerido's, its property 'EnableClientScript' should be set to false.

Hopefully, this will get you going and solve your problem.

Respondido 12 Abr '12, 11:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.