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;
        }
    }
}
4

1 回答 1

2

ASP.NET 服务器端验证的有趣之处在于它不会自动阻止您的单击事件执行。就好像执行了验证过程,然后忽略了结果。因此,首先要放入按钮事件中的是if (!Page.IsValid) return;. 这就是您阻止事件其余部分执行的方式,也是您强制用户更正表单上的任何错误的方式。

于 2013-02-21T17:58:18.973 回答