views:

510

answers:

5

This is as much a code maintenance issue as a code issue, but I have a WebForm that no longer checks it CustomValidator. It worked when I last touched the code over a year ago, but it no longer works now that the user has requested some changes ...

The WebForm contains a data-bound drop down with a default " - All -" item with String.Empty as its value. When the user clicks the submit button, the validator should check that the drop down's value is not String.Empty. I've set break points in the client validation code and the server validation code, but neither fire.

Where would you start looking? What are the usual suspects? I have, of course, compared my working copy to what is in source control, but nothing jumps out as being suspicious.

Just in case it matters, here is 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>
A: 

Some troubleshooting steps:

  • Is this the only validator on the form?
  • Is validation enabled on the page?
  • Is validation enabled for the targeted control?
  • Is the validator itself enabled?
Joel Coehoorn
A: 

I would take a serious look at the ValidationGroup.

If something has been left out of the group, it wouldn't validate anymore. Otherwise, make sure that you don't have any javascript error (for the client side) and that the method that is "OnServerValidate" has a break point inside.

Maxim
A: 

Is the validator in the same validator group as the submit button?

FlySwat
+4  A: 

I notice a couple of issues

  • I don't think you need a CausesValidation=true if AutoPostBack is set to false
  • You do not use validation groups, so that cannot be the cause
  • Why not use a RequiredFieldValidator?
  • If you want to fire validation on empty fields, set the ValidateEmptyText property to true
devio
ValidateEmptyText does the trick, but I wonder why it was not a problem last year. Has the behavior of ValidateEmptyText changed? If not, my default value must have changed.
flipdoubt
Thanks a lot, the ValidateEmptyText was causing my problem too.
JoshBaltzell
+1  A: 

A CustomValidator doesn't fire if the control it is validating has an empty value, so a CustomValidator should always be accompanied by RequiredFieldValidator

AndreasKnudsen