简体   繁体   English

动态添加RegularExpressionValidator C#

[英]Dynamically Add RegularExpressionValidator C#

I have this FileUpload control that automatically adds a FileUpload Control. 我有这个FileUpload控件,它会自动添加一个FileUpload控件。 But I need to have it also create an RegularExpressionValidator . 但我还需要创建一个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. 如果不需要UI中的控件,请通过javascript禁用正则表达式验证,并使控件的显示为无。
  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. 如果需要在UI中显示控件,请通过javascript启用正则表达式验证,并使控件出现。

Good Reference to read for this: http://msdn.microsoft.com/en-us/library/aa479045.aspx 为此的良好参考: http : //msdn.microsoft.com/zh-cn/library/aa479045.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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