1

Don't know what is wrong here. This is a page developed by someone else and I am trying to fix one of the issue.

Scenario:

ASP.NET site.

Login.aspx has <asp:login> and there are three validation groups. Login.aspx.cs is a partial class of "user_login".

Each validation group has a textbox and an associate customvalidator. All three custom validators gets triggered when something is entered in corresponding textbox but issue is only the first textbox (bound to validationgroup = 1) returns false when validation fails.

For 2nd and 3rd, the customvalidator get triggered but when there is validation issue and even after setting "args.IsValid = false;", the process continues with what needs to be executed further.

Don't know what is going on wrong here. I would like the customvalidator to return false. Worst case, are there any ways to return the control back to the 'textbox' when validation fails?

Below is the custom validator used.

<asp:CustomValidator ID="ExistingIdValidator" runat="server" ControlToValidate="NewUserName" 
    ValidateEmptyText="true" ValidationGroup="NewUserForm" 
    OnServerValidate="NewUserNameCheck_ServerValidate">
</asp:CustomValidator> 


protected void NewUserNameCheck_ServerValidate(object source, ServerValidateEventArgs args)
{
    CustomValidator validator = source as CustomValidator;
    if (validator != null)
    {
        string NewuserNameValue = this.NewUserName.Text;
        Guid registeredUserId = (Guid)Membership.GetUser(NewuserNameValue).ProviderUserKey;
        if (registeredUserId != null)
        {
            validator.IsValid = false;
            this.FailureText_New.Text = "This UserID already exists. Please login as existing user";
            args.IsValid = false;
        }
    }
}
Icarus
  • 63,293
  • 14
  • 100
  • 115
Anirudh
  • 581
  • 5
  • 14
  • 32
  • the code is not displaying here.. still trying – Anirudh Jan 27 '12 at 21:39
  • @jadarnel27:I don't see if you modified my code. If it is the custom-validator code in my original post, it is not working for me still. – Anirudh Jan 27 '12 at 22:25
  • Could you show the javascript function NewUserNameCheck_ServerValidate? – John Pick Jan 28 '12 at 00:07
  • Nevermind. I was confusing OnServerValidate with OnClientValidate. – John Pick Jan 28 '12 at 00:27
  • Could you show the definition of NewUserNameCheck_ServerValidate? – John Pick Jan 28 '12 at 00:27
  • @JohnPick: I just pasted the code above. It is not formatting for me. Please review and update. The control does come inside as "this.FailureText_New.Text" does get filled and shows. – Anirudh Jan 30 '12 at 14:33
  • If anyone has any suggestions/comments, please update me. – Anirudh Jan 30 '12 at 15:24
  • What happens if you comment out the line `validator.IsValid = false;`? That looks a little...odd to me. – Josh Darnell Jan 30 '12 at 16:39
  • @jadarnel27 : I tried commenting but makes no difference. – Anirudh Jan 30 '12 at 22:51
  • Does the submit button's event check `Page.IsValid`? – John Pick Feb 06 '12 at 16:25
  • @JohnPick:My code is pasted above and I am not doing it I guess. Where do I need to do it? – Anirudh Feb 07 '12 at 21:35
  • 1
    The funny thing about ASP.NET server-side validation is that it does not automatically prevent your click events from executing. It's as if the validation procedure is performed and then the results are ignored. So, the first thing to put inside your button's event is `if (!Page.IsValid) return;`. That's how you prevent the rest of the event from executing, and that's how you force the user to correct any mistakes on the form. – John Pick Feb 08 '12 at 17:03
  • @JohnPick: I would mark this question as answered. Can you post the above comments as a suggested answer and I will mark it as closed. – Anirudh Feb 21 '13 at 16:13

1 Answers1

2

The funny thing about ASP.NET server-side validation is that it does not automatically prevent your click events from executing. It's as if the validation procedure is performed and then the results are ignored. So, the first thing to put inside your button's event is if (!Page.IsValid) return;. That's how you prevent the rest of the event from executing, and that's how you force the user to correct any mistakes on the form.

John Pick
  • 5,562
  • 31
  • 31
  • Thanks John. This saved me a lot of time. Presumably the other (non-custom) validators on the page also have a dynamic check in JavaScript which prevents the button posting back to the server at all and thus hides this behaviour. – phn Dec 11 '19 at 13:58