[英]ASP.Net Server Side Custom Validator control help needed
I have a page with a textbox control, a Custom Validator, a button to save entered data and code to handle the custom validation. 我有一个带有文本框控件的页面,一个自定义验证器,一个用于保存输入的数据的按钮和用于处理自定义验证的代码。
I set up a simple code test just to see how the Custom Validators work. 我设置了一个简单的代码测试,以查看自定义验证器的工作方式。 I hope to add more validations that check multiple controls later.
我希望添加更多的验证,以便以后检查多个控件。 The same thing happens if I do add the ControlToValidate attribute for the textbox control.
如果我确实为文本框控件添加ControlToValidate属性,则会发生相同的事情。 (I don't think I need a "ControlToValidate" attribute for this. I plan to validate multiple controls later. I can't put all the controls I am validating in that attribute.)
(我认为我不需要“ ControlToValidate”属性。我计划以后验证多个控件。我无法将要验证的所有控件都放在该属性中。)
When I run my app, the save takes place and the validation is happeing - the message appears. 当我运行我的应用程序时,将进行保存并进行验证,消息将出现。 I don't understand why the save isn't stopped when I enter "3" in the textbox I am checking.
我不明白为什么在要检查的文本框中输入“ 3”时保存没有停止。 If the validation is happening, and if the IsValid = false, why is the save taking place?
如果正在进行验证,并且IsValid = false,那么为什么进行保存?
Here is the Custom Validator: 这是自定义验证器:
<asp:CustomValidator ID="VisitSaveCustomValidator" runat="server" OnServerValidate="VisitSaveCustomValidator_ServerValidate" ValidationGroup="SaveVisit_val"></asp:CustomValidator>
Here is the button: 这是按钮:
<asp:Button ID="SaveVisit_btn" runat="server" Visible="false" Text="- Save Visit -" ValidationGroup="SaveVisit_val" OnClick="SaveVisit_btn_Click" />
Here is the code for the Custom Validator: 这是自定义验证器的代码:
protected void VisitSaveCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
if (VisitNumber_tbx.Text == "3")
{
args.IsValid = false;
VisitSaveCustomValidator.ErrorMessage = "The Visit Number cannot be 3.";
}
else
{
args.IsValid = true;
}
Please let me know if I need to add more code or more information. 如果需要添加更多代码或更多信息,请告诉我。 I thought this would be pretty straight-forward.
我认为这很简单。 I followed an example in a book and some online.
我在书中跟随了一个例子,在网上也做了一些。 I understand that the page is going back to the server to be validated.
我了解该页面将返回到待验证的服务器。 But, shouldn't the save be stopped since the IsValid = false?
但是,由于IsValid = false,是否不应该停止保存?
It seems like the save is happening first, then the validation code executes, which causes the message to appear. 似乎先进行保存,然后执行验证代码,这导致消息出现。
Thanks. 谢谢。
I believe you may have to manually call the validate method. 我相信您可能必须手动调用validate方法。
VisitSaveCustomValidator.Validate();
Then check to see if it was valid. 然后检查它是否有效。
VisitSaveCustomValidator.IsValid();
This can be put in the button click event. 可以将其放置在按钮单击事件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.