asp.net 自定义验证器未针对文本框触发
我有一个必需的字段验证器和自定义验证器来验证文本框。所需的字段验证器完美触发。我无法让自定义验证器正确触发?
<asp:TextBox ID="txtPRI" runat="server" Width="295" /><br />
<asp:RequiredFieldValidator display="Dynamic" CssClass="leftAlign" SetFocusOnError="true" runat="server" controltovalidate="txtPRI" errormessage="Please enter your PRI" />
<asp:CustomValidator runat="server" id="cusCustom" controltovalidate="txtPRI" onservervalidate="cusCustom_ServerValidate" Enabled="true" ValidateEmptyText="true" display="Dynamic" CssClass="leftAlign" SetFocusOnError="true" errormessage="The text must be exactly 8 characters long!" />
代码隐藏
protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
{
Response.Write("firing - test");
Response.End();
if (e.Value.Length == 8)
e.IsValid = true;
else
e.IsValid = false;
}
I have both a required field validator and custom validator for validating a texbox. The required field validator fires perfectly. I'm not able to get the custom validator to fire properly?
<asp:TextBox ID="txtPRI" runat="server" Width="295" /><br />
<asp:RequiredFieldValidator display="Dynamic" CssClass="leftAlign" SetFocusOnError="true" runat="server" controltovalidate="txtPRI" errormessage="Please enter your PRI" />
<asp:CustomValidator runat="server" id="cusCustom" controltovalidate="txtPRI" onservervalidate="cusCustom_ServerValidate" Enabled="true" ValidateEmptyText="true" display="Dynamic" CssClass="leftAlign" SetFocusOnError="true" errormessage="The text must be exactly 8 characters long!" />
code behind
protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
{
Response.Write("firing - test");
Response.End();
if (e.Value.Length == 8)
e.IsValid = true;
else
e.IsValid = false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
检查您的
CustomValidator
属性ValidateEmptyText
设置为true
,以便验证空文本。那么您将不再需要RequiredFieldValidator
。编辑:我获取了您的代码并将其复制并粘贴到一个空项目中,它按预期工作。一定有一些我们不知道的您未发布或发布错误的内容。还有其他什么会影响触发验证的按钮或验证控件本身吗?
编辑:这是确切的代码(位于内容页面中):
aspx页面:
.cs页面(空
Page_Load
):Check that you have the your
CustomValidator
propertyValidateEmptyText
set totrue
so that empty text will be validated. Then you will not need theRequiredFieldValidator
anymore.EDIT: I took your code and copy and pasted it into an empty project and it works as expected. There must be something you have not posted, or is posting incorrectly, that we are not aware of. Is there anything else that affects the button that is triggering the validation or the validation controls themselves?
EDIT: Here is the exact code (it's in a content page):
aspx page:
.cs page (empty
Page_Load
):好吧......非常老的问题,还没有被接受的答案,我刚刚遇到了同样的问题。
因此,我将把这个问题抛给可能遇到此问题并需要答案的其他人...
如果您正在执行常规验证以及自定义服务器验证,则只有在所有其他验证都已完成的情况下,自定义服务器验证才会触发至少干净地回来,这对我来说就是这样。
Ok... really old question with no accepted answer yet, and I just ran into the same issue.
So I'm going to throw this out there for anyone else who might have this problem and needs an answer...
If your're doing regular validations, along with custom server validations, the custom server validation will only fire if all other validations come back clean, at least, that's the way it's worked for me.
请记住在 CustomValidator 上设置此属性...
Remember to set this property on the CustomValidator...
除了上面的建议之外,我发现使用较新版本的 .Net 框架,您必须在服务器上显式触发 validate() 方法才能获取自定义验证器例程
In addition to the suggestions above, I've found that with newer versions of the .Net framework you must explicitly fire the validate() method at the server to get the custom validator routine
我也有这个问题。是的,所有其他验证器都必须在
CustomValidator
触发之前通过。但是,如果这对您不起作用,那么您可能需要使用
Page.Validate()
强制验证您的特定验证组。我就是这样做的,并且我仍然设法保留了我的
RequiredFieldValidator
并且不需要ValidateEmptyText="true"
。在文本框中添加陷阱以强制验证。
请注意,我正在使用特定的
ValidationGroup
“vg2”,因为我还有其他不想验证的区域。另外,我想验证日期&时间!
你还需要两件事。
TextBoxChanged_DateTimeTest
方法...并且您还需要对
CustomValidator
进行服务器端验证。就我而言,文本框必须接受日期和日期。时间!这是标记...
这是背后的代码...
祝你好运!
I had this problem too. Yes all other validators have to pass before the
CustomValidator
fires.However, if that does not work for you, then you may need to force a validate of your specific validation group using the
Page.Validate()
.This is how I did it, and I still managed to keep my
RequiredFieldValidator
and no need forValidateEmptyText="true"
.Add a trap in the textbox to force a validate.
Note that I am using a specific
ValidationGroup
"vg2", as I have other areas that I don't want to validate.Also, I want to validate date & time!
You need two more things. The
TextBoxChanged_DateTimeTest
method ...And you also need the server side validate for the
CustomValidator
. In my case the TextBox has to accept a date & time!So here's the markup...
And here's the code behind ...
Good luck!
问题是您正在调用
Response.End()
可有效停止页面的所有执行。因此,if/else 块根本没有运行。在调试时注释掉该行或跳过它,验证器将按预期触发。我建议您使用调试器,而不是以这种方式编写响应,或者如果您选择使用它,请注意 Response.End() 的后果。
The problem is that you're calling
Response.End()
which effectively stops all execution of the page. Thus, the if/else block isn't being run at all. Comment that line out or skip over it while debugging and the validator will fire as expected.I suggest you use a debugger instead of writing responses out in this fashion or be aware of the consequences of
Response.End()
if you choose to use it.据我记得,如果您的文本框为空,自定义验证器将不会触发。
As far as I remember, custom validator will not fire if your textbox is empty.
如果代码中有多个部分并且每个部分都有一些验证器,请为每个部分创建 validateGroup
if you have multiple part in your code and each part has some validator, create validateGroup for each part