简体   繁体   English

自定义验证器触发,但它不更新ValidationSummary

[英]Custom Validator firing but it does not update the ValidationSummary

Hi I am working on a custom form field validator, it seems like the custom validator is working by not allowing it to continue to the next page, but it doesn't update the Validation Summary nor does it display the asterisk and the labels that i've made visable. 嗨我正在使用自定义表单字段验证器,似乎自定义验证器正在工作,不允许它继续到下一页,但它不更新验证摘要也不显示星号和标签,我已经变得可见了。 I also have other validators like RequiredFieldValidator on the same field. 我还在同一个字段上有其他验证器,如RequiredFieldValidator。 My ValidationGroup is set, as is the Text and IsValid. 设置了我的ValidationGroup,Text和IsValid也是如此。 I even wrote and set a dummy client side validation method in javascript as some workarounds suggests. 我甚至在javascript中编写并设置了一个虚拟客户端验证方法,正如一些解决方法所暗示的那样。

here is the validation summary code in asp.net 这是asp.net中的验证摘要代码

<asp:ValidationSummary ID="ValidatorSummary" runat="server" ValidationGroup="Step2" />

here is the custom validator and the required field one 这是自定义验证器和必填字段

<asp:CustomValidator ID="AddressVerification" runat="server" ErrorMessage="Please enter a valid address." Display="Dynamic" ValidationGroup="Step2" OnServerValidate="AddressVerification_ServerValidate" ClientValidationFunction="CustomValidatorDummy" Text="*" Enabled="true" EnableClientScript="true"></asp:CustomValidator>
<asp:RequiredFieldValidator ID="RFValidatorHomeAddress" runat="server" ErrorMessage="Please enter home address." Text="*" Display="Dynamic" ValidationGroup="Step2" ControlToValidate="txtHomeAddress"></asp:RequiredFieldValidator>

here is the custom validation method in the code behind 这是后面代码中的自定义验证方法

protected void AddressVerification_ServerValidate(object sender, ServerValidateEventArgs e)
{
//lets just say it doesn't validate and sets the IsValid to false
lblUspsValidatorResHomeCity.Visible = true;
lblUspsValidatorResHomeState.Visible = true;
lblUspsValidatorResHomeZip.Visible = true;
e.IsValid = false;
}

please advise, thanks. 请指教,谢谢。

EDIT: Answered - as bitxwise mentioned. 编辑:回答 - 按照bitxwise提到。 the validation summary should be placed inside an update panel as well. 验证摘要也应放在更新面板中。 Thanks! 谢谢!

Like so: 像这样:

<asp:UpdatePanel ID="UpdatePanelValidationSummaryHome" ChildrenAsTriggers="false" UpdateMode="Conditional"
runat="server">
<ContentTemplate>
    <asp:ValidationSummary ID="AddressHomeValidationSummary" runat="server" ValidationGroup="AddressHomeValidationGroup"
        CssClass="errors" /> 
</ContentTemplate>

and then calling the update: 然后调用更新:

UpdatePanelValidationSummaryHome.Update();

You seem to be missing ControlToValidate in your declaration of CustomValidator . 您似乎在CustomValidator声明中缺少ControlToValidate

EDIT 编辑

If your CustomValidator aggregates multiple controls, then try this: 如果您的CustomValidator聚合多个控件,请尝试以下操作:

ASPX ASPX

<asp:TextBox ID="txtMyTextBox" runat="server" />
<asp:CustomValidator ID="AddressVerification" runat="server"
    Display="Dynamic"
    ErrorMessage="Please enter a valid address."
    OnServerValidate="AddressVerification_ServerValidate"
    Text="*"
    ValidationGroup="Step2" />
<asp:RequiredFieldValidator ID="rfvAddress" runat="server"
    ControlToValidate="txtMyTextBox"
    Display="Dynamic"
    ErrorMessage="Please enter an address"
    Text="*"
    ValidationGroup="Step2" />
...
<asp:ValidationSummary ID="ValidatorSummary" runat="server"
    ValidationGroup="Step2" />
...
<asp:Button ID="btnCheckAddresses" runat="server"
    CausesValidation="true"
    Text="Check Addresses"
    ValidationGroup="Step2" />

CS CS

protected void AddressVerification_ServerValidate(object source, ServerValidateEventArgs args) {
    args.IsValid = !string.IsNullOrEmpty(txtMyTextBox.Text) && !txtMyTextBox.Text.Contains(' ');
}

Note that the validation group of the control invoking the post back has CausesValidation="true" and has the same ValidationGroup as the validators. 请注意,调用回发的控件的验证组具有CausesValidation="true"并且具有与ValidationGroup器相同的ValidationGroup

EDIT 2 编辑2

If your postback control was in the UpdatePanel but the ValidationSummary was not, then the partial postback would not have refreshed the ValidationSummary . 如果您的回发控件位于UpdatePanelValidationSummary不在,则部分回发将不会刷新ValidationSummary Once you removed the postback control from the UpdatePanel , I imagine it would then generate a full postback, which would refresh your ValidationSummary . 一旦你从UpdatePanel删除了回发控件,我想它会生成一个完整的回发,这将刷新你的ValidationSummary

I don't know what else is in your UpdatePanel , but many people report having issues with their validators being in UpdatePanel 's . 我不知道你的UpdatePanel还有什么,但很多人报告他们的验证器在UpdatePanel中存在问题

Check out MSDN , 查看MSDN

When you use the ValidationSummary control inside an UpdatePanel control, make sure that the validator control and the control it is associated with are in the same panel. 在UpdatePanel控件中使用ValidationSummary控件时,请确保验证器控件及其关联的控件位于同一面板中。 For more information about using the UpdatePanel control for partial-page updates, see Partial-Page Rendering Overview. 有关将UpdatePanel控件用于部分页面更新的详细信息,请参阅部分页面呈现概述。

as well as this MSDN blog . 以及这个MSDN博客

Make sure every control (textbox, checkbox, etc) that is being validated, every RequiredValidator, CustomValidator and ValidationSummary has the same ValidationGroup value. 确保正在验证的每个控件(文本框,复选框等),每个RequiredValidator,CustomValidator和ValidationSummary都具有相同的ValidationGroup值。

ie. 即。

<asp:CustomValidator ID="CustomValidator6" runat="server" ErrorMessage="The field is required"
ValidationGroup="myValGroup">*</asp:CustomValidator>

Of course this will only work if all the controls are inside the same panel or parent control. 当然,这只有在所有控件都位于同一面板或父控件内时才有效。

In my case the validation summary was not showing because the submit button was in a separate update panel. 在我的情况下,验证摘要未显示,因为提交按钮位于单独的更新面板中。

<Triggers>                        
<asp:PostBackTrigger ControlID="ButtonSubmit" />
</Triggers>

Once I added the above code the summary appeared. 一旦我添加了上面的代码,就会出现摘要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM