3

Is there anyway to run an event after i selected a file in a Fileupload control, so i can set Label1.Text = FileUpload.FileName;

Or if any of you got another idea that would be awesome too(maybe some javascript)! :)

1 Answer 1

4

You can listen for the change event on the client side. Here's the syntax for IE but you can adapt it for the better browsers.

    <asp:FileUpload ID="FileUpload1" runat="server" /> <span id="txt" />
    <script>
        var fu = document.getElementById('<% =FileUpload1.ClientID %>');
        fu.attachEvent('onchange', function (e) {
            document.getElementById('txt').innerHTML = e.srcElement.value;
        });
    </script>

I'm pretty sure good browsers will report only the filename, where IE with report the full path too (incorrectly).

Sign up to request clarification or add additional context in comments.

1 Comment

@Fogh are you calling addEventListener instead of attachEvent for browsers that require it? are you getting a proper reference to the element in browsers that don't support srcElement?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.