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();
You seem to be missing
ControlToValidate
in your declaration ofCustomValidator
.EDIT
If your CustomValidator aggregates multiple controls, then try this:
ASPX
CS
Note that the validation group of the control invoking the post back has
CausesValidation="true"
and has the sameValidationGroup
as the validators.EDIT 2
If your postback control was in the
UpdatePanel
but theValidationSummary
was not, then the partial postback would not have refreshed theValidationSummary
. Once you removed the postback control from theUpdatePanel
, I imagine it would then generate a full postback, which would refresh yourValidationSummary
.I don't know what else is in your
UpdatePanel
, but many people report having issues with their validators being inUpdatePanel
's.Check out MSDN,
as well as this MSDN blog.
Make sure every control (textbox, checkbox, etc) that is being validated, every RequiredValidator, CustomValidator and ValidationSummary has the same ValidationGroup value.
ie.
Of course this will only work if all the controls are inside the same panel or parent control.