12
votes

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. Everything looks alright to me. 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 - 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:

5
if you add a breakpoint it doesn't break into? - Roberto Alarcon
No, as stated in the question, breakpoints in the server validation event cvVendorBriefFile_ServerValidate do not get hit. Breakpoints in the button click event do get hit. - Chev
The two answers provided right now are wrong, I made a lab with your code and it work, you have something else, maybe in the client side. the code break into the validation when I press the button. Try it, copy paste your code in a fresh page. (update: the answers are not wrong, they just don't solve your question) - Roberto Alarcon
Interesting. I'm going to have to play with this further. I just did the same thing and you are correct. Everything works fine in a fresh page. I'm stumped. - Chev
I added some links to full source at the end of the question. - Chev

5 Answers

22
votes

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! :)

1
votes

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

1
votes

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.

0
votes

Add CausesValidation="True" to your Button declaration.

0
votes

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?