views:

53

answers:

1

I have a FileUpload control along with a required field validator. It throws an error if the user doesn't click the Browse button to select a file (which is correct). However, if the user clicks the Browse button, but doesn't click the Upload button, ASP.NET's required validator doesn't throw an error. Any ideas how to fix?

+2  A: 

Why not use a CustomValidator instead of a RequiredFieldValidator?

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = FileUpload1.PostedFile.ContentLength != 0;
    }

    private void Save()
    {
        if (Page.IsValid)
        {
            var myFileName = "somefile.jpg"
            FileUpload1.PostedFile.SaveAs(myFileName);
        }
    }
darkfreq