自定义验证器
所以我有一个只能部分工作的自定义验证器。它基本上检查两件事:是否填写了两个字段,以及这些字段中输入的内容是否已存在于数据库中。检查数据库工作正常,但检查字段是否填写则不然。我不想使用必填字段验证器,因为我希望错误消息全部位于页面上完全相同的位置。我很确定我只是搞砸了一些简单的事情,但我就是找不到它。
<strong>Course Prefix and Number:</strong>
<asp:TextBox ID="txtCoursePrefix" runat="server" Width="45" MaxLength="4" CssClass="caps"></asp:TextBox>
-
<asp:TextBox ID="txtCourseNum" runat="server" Width="45" MaxLength="6" CssClass="caps"></asp:TextBox>
<span class="required">*
<asp:CustomValidator ID="cvDuplicate" runat="server" ControlToValidate="txtCoursePrefix" ValidateEmptyText="true"></asp:CustomValidator>
</span>
隐藏代码:
'Check if fields have been filled out
If txtCoursePrefix.Text Is Nothing Or txtCourseNum.Text Is Nothing Then
cvDuplicate.ErrorMessage = "Required"
args.IsValid = False
End If
'Code that checks values against database goes here
'If matching record does not exist...
If myValue IsNot Nothing Then
cvDuplicate.ErrorMessage = "Course number is already taken."
args.IsValid = False
End If
所以,第一部分再次不起作用,第二部分工作正常。
So I have a custom validator that is working only partially. It basically has two things it's checking for: if two fields are filled out, and whether or not what has been entered in those fields already exists in the database. Checking against the database is working fine, but checking whether or not the fields are filled out is not. I don't want to use required field validators, since I want the error messages all in the exact same location on the page. I'm pretty sure I just messed up on something simple, but I just can't find it.
<strong>Course Prefix and Number:</strong>
<asp:TextBox ID="txtCoursePrefix" runat="server" Width="45" MaxLength="4" CssClass="caps"></asp:TextBox>
-
<asp:TextBox ID="txtCourseNum" runat="server" Width="45" MaxLength="6" CssClass="caps"></asp:TextBox>
<span class="required">*
<asp:CustomValidator ID="cvDuplicate" runat="server" ControlToValidate="txtCoursePrefix" ValidateEmptyText="true"></asp:CustomValidator>
</span>
Code behind:
'Check if fields have been filled out
If txtCoursePrefix.Text Is Nothing Or txtCourseNum.Text Is Nothing Then
cvDuplicate.ErrorMessage = "Required"
args.IsValid = False
End If
'Code that checks values against database goes here
'If matching record does not exist...
If myValue IsNot Nothing Then
cvDuplicate.ErrorMessage = "Course number is already taken."
args.IsValid = False
End If
So once again it is the first part that's not working, the second part is working fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
文本框可能被视为空字符串,而不是空字符串。尝试一下这个检查:
It might be that the Text box is considered an empty string, not Nothing. Try this for your check:
需要检查 2 件事
添加以下FieldRequired="True"
this.cvDuplicate.Enabled = false;
如果将以下属性设置为 false。即使您从服务器端的数据库进行设置,也不会进行验证。所以设置这些时要小心。如果您没有在 .ASCX 上设置这些属性并尝试从服务器端的数据库(C# 代码)设置它们,那么它们将起作用。
this.cvDuplicate.Enabled = true;
ValidateEmptyText="False"
FieldRequired="False"
2 things needs to be checked
Add following FieldRequired="True"
<asp:CustomValidator ID="cvDuplicate" runat="server" ControlToValidate="txtCoursePrefix" ValidateEmptyText="true" FieldRequired="True"></asp:CustomValidator>
On Server Side check if any place following is setup
this.cvDuplicate.Enabled = false;
If you set following property as false. No validation will occur even if you set it up from Database on server side. So be careful when you set these up. If you do not set up these properties on .ASCX and set try to set them up from Database on server side (C# code) then they will work.
this.cvDuplicate.Enabled = true;
ValidateEmptyText="False"
FieldRequired="False"