2010-06-06 7 views

답변

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