我有这个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>
答案 0 :(得分:0)
也许你可以尝试这种方式:
- 始终在页面中启动正则表达式验证器以及控件。
- 通过javascript禁用正则表达式验证,如果不需要控件,则将控件的显示设置为无 存在于用户界面中。
- 通过javascript启用正则表达式验证,并在需要控件存在时使控件出现 在用户界面中。
醇>
良好参考阅读: http://msdn.microsoft.com/en-us/library/aa479045.aspx