ASP.NET Web-forms custom validator not firing

I have a custom validator on my page for a file upload control.

<asp:FileUpload ID="fuVendorBrief" runat="server" />
<br />
<asp:CustomValidator ID="cvVendorBriefFile" Display="Dynamic" runat="server" ValidationGroup="EditorValidate" ControlToValidate="fuVendorBrief" OnServerValidate="cvVendorBriefFile_ServerValidate" ErrorMessage="You must upload a vendor brief PDF file.">     
</asp:CustomValidator>

I then also have a button.

<asp:Button ID="btnSubmit" ValidationGroup="EditorValidate" OnClick="btnSubmit_Click" runat="server" Text="Add Vendor Brief" />

I have defined my custom validator event like so...

protected void cvVendorBriefFile_ServerValidate(object source, ServerValidateEventArgs args)
{
    CustomValidator fileUploadValidator = (CustomValidator)source;
    FileUpload vendorBriefFileUpload = (FileUpload)fileUploadValidator.Parent.FindControl(fileUploadValidator.ControlToValidate);
    args.IsValid = vendorBriefFileUpload.HasFile && vendorBriefFileUpload.FileName.ToLower().EndsWith(".pdf");
}

This custom validator isn't even getting fired. Maybe I'm just tired, but everything looks alright to me so an insight is appreciated. If I drop a breakpoint anywhere in the server validation event it does not get hit when I click submit. I can hit breakpoints in the submit button's click event however.

Any ideas?

EDIT - Forgot to mention that I have other validation controls, like required field validators, on the page and they fire just fine.

EDIT 2 - If you want the full source of the page and its codebehind then follow these links:

Let me know if you see anything weird. I'm half asleep over here and I'm hoping I'm just missing something dumb.

Answers


Try removing ControlToValidate entirely. Though I've never tried to validate a file upload before, most validators won't fire (except RequiredField) if the contents are empty. Taking off the control to validate should make it fire always for that group.

EDIT (Chevex) - The ControlToValidate was the issue, but not because it was broken. By default it will not fire on controls with no value, as stated above. Setting the custom validator control property ValidateEmptyText="true" solves the issue. Sad that I had to start this giant question just to find that, but now we know! :)


You need to specify the same ValidationGroup="" to your button, and to your validators


For me this occurred when the validator and its related input were inside a control that had visible="false" set in the control mark up. This was causing the CustomValidator to inherit the Visible = false property and preventing validation from firing. On a normal page load I wasn't making the controls visible until later in the page lifecycle.

In any case if you set a breakpoint on the Page.Validate() method you can inspect the Page.Validators collection and see if a similar thing might be happening to you.


Add CausesValidation="True" to your Button declaration.


If you look to the documentation at http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(%22ASP%3aCUSTOMVALIDATOR%22);k(VS.HTMLDESIGNER.HTML);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV3.5%22);k(DevLang-ASPX)&rd=true

you see

When using validator controls, you should always check the results of server-side validation before performing any processing. After a postback but before your event methods are called, the page calls the validator controls and aggregates their results into the Page.IsValid property. (You can also call the validator controls explicitly using the Validate method.) In your own code, you should check that the Page.IsValid property returns true before processing input. Even though script-enabled browsers might prevent a postback from occurring on the client if a validation check has failed, you should always also check Page.IsValid in server code before processing validated data.

So, are in testing for Page.IsValid in your Page Load?


Need Your Help

Malloc a 3-Dimensional array in C?

c arrays multidimensional-array

I'm translating some MATLAB code into C and the script I'm converting makes heavy use of 3D arrays with 10*100*300 complex entries. The size of the array also depends on the sensor's input, ideally...

list() uses more memory than list comprehension

python list list-comprehension cpython python-internals

So i was playing with list objects and found little strange thing that if list is created with list() it uses more memory, than list comprehension? I'm using Python 3.5.2