简体   繁体   中英

Dynamically Add RegularExpressionValidator C#

I have this FileUpload control that automatically adds a FileUpload Control. But I need to have it also create an RegularExpressionValidator . Not sure how I can do this.

Any ideas?

<script type = "text/javascript">
var counter = 0;
function AddFileUpload() {
var div = document.createElement('DIV');
div.innerHTML = '<input id="file' + counter + '" name = "file' + counter +
 '" type="file" />' +
  '<input id="Button' + counter + '" type="button" ' +
  'value="Remove" onclick = "RemoveFileUpload(this)" />';
    document.getElementById("FileUploadContainer").appendChild(div);
    counter++;
}
function RemoveFileUpload(div) {
    document.getElementById("FileUploadContainer").removeChild(div.parentNode);
}
</script>  


<div id="FileUploadContainer">
     <asp:FileUpload ID="FileUpload1" runat="server" /> 

     <!--FileUpload Controls will be added here -->

</div>    
<br />

 <input id="Button1" onclick="AddFileUpload()" style="height: 27px; width: 150px;" tabindex="25" type="button" value="Add More Attachments" />

 <asp:RegularExpressionValidator ID="rexp" runat="server" ControlToValidate="FileUpload1"
 ErrorMessage="Only .pdf, .jpg"
 ValidationExpression="(.*\.([Pp][Dd][Ff])|.*\.([Jj][Pp][Gg])$)">
 </asp:RegularExpressionValidator>  

Probably you could try this way:

  1. Always initiate Regular Expression Validator in the page along with the control.
  2. Make the Regular Expression Validation disable through javascript and make the control's display to none if you don't need the control presence in the UI.
  3. Make the Regular Expression Validation enable through javascript and make the control's to be appeared if you need the control presence in the UI.

Good Reference to read for this: http://msdn.microsoft.com/en-us/library/aa479045.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM