페이지를 전송하기 전에 사용자가 파일을 선택하도록 강제하기 위해 클라이언트 측 필수 유효성 검사기를 asyncfileupload에 추가하는 방법.ajax AsyncFileUpload에 필요한 유효성 검사기를 추가하는 방법은 무엇입니까?
3
A
답변
1
보이지 않는 TextBox의 유효성을 검사하는 RequiredFieldValidator를 사용합니다. 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>
2
또한 C# 또는 VB보다는 클라이언트 측 자바 스크립트 또는 JQuery와 함수를 사용하여 서버 측 방법의 숨겨진 텍스트 상자의 텍스트를 설정할 수 있습니다.
protected void afu_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
afu.SaveAs(Server.MapPath("Uploads\\") + e.FileName);
txt.Text = e.FileName;
}
더 나은 태깅을하십시오! –