asp:CustomValidator 不返回“false”
不知道这里出了什么问题。这是由其他人开发的页面,我正在尝试解决其中一个问题。
场景:
ASP.NET 网站。
Login.aspx 有
并且有三个验证组。 Login.aspx.cs 是“user_login”的部分类。
每个验证组都有一个文本框和一个关联的自定义验证器。当在相应的文本框中输入内容时,所有三个自定义验证器都会被触发,但问题是只有第一个文本框(绑定到validationgroup = 1)在验证失败时返回 false。
对于第二个和第三个,自定义验证器被触发,但是当存在验证问题时,即使在设置“args.IsValid = false;”之后,该过程也会继续需要进一步执行的操作。
不知道这里出了什么问题。我希望 customvalidator 返回 false。最坏的情况是,当验证失败时,有什么方法可以将控件返回到“文本框”吗?
下面是使用的自定义验证器。
<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;
}
}
}
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ASP.NET 服务器端验证的有趣之处在于它不会自动阻止单击事件的执行。就好像执行了验证过程然后结果被忽略一样。因此,要放入按钮事件中的第一件事是
if (!Page.IsValid) return;
。这就是阻止事件其余部分执行的方法,也是强制用户更正表单上任何错误的方法。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.