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>
source to share
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
source to share