locked
Updating Textbox on File Upload RRS feed

  • Question

  • User1396448631 posted

    I have a AjaxFileUpload control on my page which successfully uploads file when its default upload button is clicked.

    Later on, I thought of adding a textbox which would show the name of the file uploaded.

     <asp:ScriptManager ID="ToolkitScriptManager1" runat="server"> </asp:ScriptManager>   
     <asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server" 
            OnUploadComplete="AjaxFileUpload1_OnUploadComplete" 
            MaximumNumberOfFiles="1"
            AllowedFileTypes="pdf"/>
    
     <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
           <asp:TextBox ID="FileTxtBox" runat="server" />
        </ContentTemplate>
     </asp:UpdatePanel>

    In codebehind,

     protected void AjaxFileUpload1_OnUploadComplete(object sender, AjaxFileUploadEventArgs file)
        {
            string fileName = Path.GetFileNameWithoutExtension(file.FileName);
            FileTxtBox.Text = fileName;
            UpdatePanel1.Update();
        }

    Do I need to do anything extra for the textbox to show the file? Please suggest.

    Thank you

    Friday, February 12, 2016 6:20 PM

Answers

  • User281315223 posted

    The code provided at least appears to be correct. Is it not working as expected?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 12, 2016 6:33 PM
  • User61956409 posted

    Hi dbqasp,

    This seems to work but I am just wondering if this is going to fail on an unknown scenario.

    According to your code, we could find that you dynamically set value for 'FileTxtBox' using JavaScript in client-side UploadComplete event, and the JavaScript methods or properties you are using are supporting in major browsers, so the code should be working fine.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 15, 2016 8:43 AM

All replies

  • User281315223 posted

    The code provided at least appears to be correct. Is it not working as expected?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 12, 2016 6:33 PM
  • User1396448631 posted

    The textbox doesn't show anything.

    So what I did is use a javascript function to pull the filename out of the span element.

    I used AjaxFileUpload's OnClientUploadComplete="populateFileTxtBox"

    and defined the function like this

    function populateFileTxtBox() {
                var div = document.getElementById("AjaxFileUpload1_QueueContainer");
                var spans = div.getElementsByClassName("filename");
    
                var fileName;
    
                for (i = 0; i < spans.length; i++) {
                    fileName = spans[i].innerHTML;
                    break;
                }
    
                document.getElementById('FileTxtBox').value = fileName;
            }

    This seems to work but I am just wondering if this is going to fail on an unknown scenario.

    Your thoughts?

    Friday, February 12, 2016 6:59 PM
  • User61956409 posted

    Hi dbqasp,

    This seems to work but I am just wondering if this is going to fail on an unknown scenario.

    According to your code, we could find that you dynamically set value for 'FileTxtBox' using JavaScript in client-side UploadComplete event, and the JavaScript methods or properties you are using are supporting in major browsers, so the code should be working fine.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 15, 2016 8:43 AM