views:

20

answers:

2

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)! :)

A: 

you can use javascript ontextchanged event, it will trigger your javascript function where you can get and assign text value

I don't have .net environment right now , but something like work..

<asp:fileupload runat="Server" id="fup" ontextchanged="javascript:assigntext()" />

javascript funtion

function assigntext()
{
document.getelementbyid('labelid').value = document.getelementbyid('fup').value;
}
Muhammad Akhtar
Hmm.. Dont seem to work..
Fogh
+1  A: 

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).

lincolnk
Seems to work only in IE..
Fogh
@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`?
lincolnk
Modified the script abit and got it working. Thanks!
Fogh