I have a FileUpload control along with a required field validator. It throws an error if the user d开发者_如何学Pythonoesn'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?
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);
}
}
精彩评论