How to put this validation on asp.net file upload control?
I have a fileUpload User Control FileUploadControl.ascx containing asp.net fileupload control and a requiredfield validator.
I have another usercontrol RenderingTemplate.ascx that contains two FileUploadControl.ascx.
RenderingTemplate.ascx is on page RenderingTemplateDetails.aspx, this page has save and cancel button.
Now My requirement is in case user selects a file in one fileupload control,he has to select in other as well. If he does not select any file,let postback happen.
I cannot use Requiredfieldvalidator as it fires for both. I tried adding onclientclick to page save button and check whether file upload has some text in it.but i didnot succedd.
Now i want this functionality to be a part of control so that i dont have to recode it on every page. IS there any clientside 开发者_StackOverflow社区event on usercontrol which fires when save button of page is clicked. Please help and suggest some solution.
The solution will become easier if you add custom validator. Take a look at how to add client validation function to custom validator.
Add custom validator to both of the file uploads and then in client validation function, place a single javascript function, Here is a simple example:
function ValidateUpload(source,args){
//assume f1 and f2 are your file upload controls:
var f1 = document.getElementById('f1'); //in case of asp.net control use clientID
var f2 = document.getElementById('f2');
if(f1.value == f2.value)
args.IsValid = true;
else
args.IsValid = false;
}
Notes: you can use single custom validator as well, or you can use source and args.Value with some additional logic to solve this but I think this is the most simple implementation I find.
精彩评论