What do you suspect when ASP.NET ignores CustomValidator?
This is a code maintenance issue as a code issue, but I have a WebForm that no longer checks its CustomValidator. It worked when I last touched the code over a year ago, but now it doesn't work anymore when the user requested some changes ...
The WebForm contains a drop-down list of data with a default of "- All -" with String.Empty as the value. When the user clicks the submit button, the validator must check that the dropdown value is not equal to String.Empty. I have set breakpoints in client validation code and server validation code, but not fire.
Where would you start looking? What are the usual suspects? I have, of course, compared my working copy with one that is under source control, but nothing seems suspicious.
It's important just in case, here's my code:
<asp:DropDownList ID="_AssessmentDropDown" runat="server" DataSourceID="_AssessmentsData" CausesValidation="true" AutoPostBack="false"
DataTextField="AssessmentName" DataValueField="AssessmentName" OnDataBound="_HandleAssessmentsBound">
</asp:DropDownList>
<asp:CustomValidator ID="_AssessmentValidator" runat="server" ClientValidationFunction="_HandleValidateAssessment_Client"
ControlToValidate="_AssessmentDropDown" ErrorMessage="* You must select an Assessment."
OnServerValidate="_HandleValidateAssessment" />
<asp:ObjectDataSource ID="_AssessmentsData" runat="server"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
TypeName="DataTableAdapters.GET_GRADE_ASSESSMENTSTableAdapter">
<SelectParameters>
<asp:ControlParameter Name="GRADECODE" ControlID="_GradeCodeDropDown" PropertyName="SelectedValue" />
</SelectParameters>
</asp:ObjectDataSource>
I notice a couple of problems
- I don't think you need CausesValidation = true if AutoPostBack is set to false
- You are not using validation groups, so this cannot be the reason
- Why not use RequiredFieldValidator?
- If you want to activate validation for empty fields, set the ValidateEmptyText property to true
CustomValidator does not fire if the control it validates is empty, so the CustomValidator parameter must always be set to RequiredFieldValidator
Some troubleshooting steps:
- Is this the only validator on the form?
- Is on-page validation enabled?
- Is validation allowed for the target control?
- Is the validator enabled?
I would take a serious look at ValidationGroup.
If something is left outside the group, it will no longer be checked. If not, make sure you don't have a javascript error (client side) and that the method that is "OnServerValidate" has a breakpoint inside.
Is the validator in the same validator group as the submit button?