动态添加RegularExpressionValidator C#

时间:2014-01-13 01:52:43

标签: c# reg-expressionvalidator

我有这个FileUpload控件,会自动添加FileUpload控件。但我需要让它创建一个RegularExpressionValidator。不知道我怎么做到这一点。

有什么想法吗?

<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>  

1 个答案:

答案 0 :(得分:0)

也许你可以尝试这种方式:

  
      
  1. 始终在页面中启动正则表达式验证器以及控件。
  2.   
  3. 通过javascript禁用正则表达式验证,如果不需要控件,则将控件的显示设置为无   存在于用户界面中。
  4.   
  5. 通过javascript启用正则表达式验证,并在需要控件存在时使控件出现   在用户界面中。
  6.   

良好参考阅读: http://msdn.microsoft.com/en-us/library/aa479045.aspx