[英]Dynamically Add RegularExpressionValidator C#
我有这个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>
可能您可以这样尝试:
- 总是与控件一起在页面中启动正则表达式验证器。
- 如果不需要UI中的控件,请通过javascript禁用正则表达式验证,并使控件的显示为无。
- 如果需要在UI中显示控件,请通过javascript启用正则表达式验证,并使控件出现。
为此的良好参考: http : //msdn.microsoft.com/zh-cn/library/aa479045.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.