Custom Validator firing but it does not update the ValidationSummary

旧巷老猫 提交于 2019-12-12 14:09:22

问题


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. My ValidationGroup is set, as is the Text and IsValid. I even wrote and set a dummy client side validation method in javascript as some workarounds suggests.

here is the validation summary code in 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. 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();

回答1:


You seem to be missing ControlToValidate in your declaration of CustomValidator.

EDIT

If your CustomValidator aggregates multiple controls, then try this:

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

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.

EDIT 2

If your postback control was in the UpdatePanel but the ValidationSummary was not, then the partial postback would not have refreshed the ValidationSummary. Once you removed the postback control from the UpdatePanel, I imagine it would then generate a full postback, which would refresh your ValidationSummary.

I don't know what else is in your UpdatePanel, but many people report having issues with their validators being in UpdatePanel's.

Check out 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. For more information about using the UpdatePanel control for partial-page updates, see Partial-Page Rendering Overview.

as well as this MSDN blog.




回答2:


Make sure every control (textbox, checkbox, etc) that is being validated, every RequiredValidator, CustomValidator and ValidationSummary has the same ValidationGroup value.

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.




回答3:


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.



来源:https://stackoverflow.com/questions/4308475/custom-validator-firing-but-it-does-not-update-the-validationsummary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!