如何向asyncfileupload添加客户端所需的验证器,以强制用户在提交页面之前选择文件。
答案 0 :(得分:2)
您还可以使用C#或VB在服务器端方法中设置隐藏文本框的文本,而不是客户端Javascript或JQuery函数。
protected void afu_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
afu.SaveAs(Server.MapPath("Uploads\\") + e.FileName);
txt.Text = e.FileName;
}
答案 1 :(得分:1)
我使用RequiredFieldValidator来验证不可见的TextBox。 TextBox在OnClientUploadComplete函数中填充了任意文本。 你唯一不能做的就是在验证时设置焦点。 该示例使用jQuery。
<ajaxToolkit:AsyncFileUpload runat="server" ID="afu" ClientIDMode="AutoID" UploaderStyle="Traditional" OnClientUploadComplete="asyncUploadComplete" OnClientUploadStarted="asyncUploadStarted" />
<asp:RequiredFieldValidator runat="server" ID="rfv" ControlToValidate="txt" Text="The file is required!" SetFocusOnError="false" />
<asp:TextBox runat="server" ID="txt" style="display:none" MaxLength="0" />
<script type="text/javascript">
// AsyncFileUpload - OnClientUploadComplete
function asyncUploadComplete(sender, args) {
// Assemble info of uploaded file
var contentType = args.get_contentType();
var info = args.get_length() + " bytes";
if (contentType.length > 0) {
info += " - " + contentType;
}
info += " - " + args.get_fileName();
// Put info in the first input field after the AsyncFileUpload control
var source = $(sender.get_element());
source.nextAll('input').val(info);
// Validate immediately
ValidatorEnable(source.nextAll('span')[0], true);
}
// AsyncFileUpload - OnClientUploadStarted
function asyncUploadStarted(sender, args) {
// Clear the first input field after the AsyncFileUpload control
var source = $(sender.get_element());
source.nextAll('input').val('');
}
</script>