The problem here is that the FileUpload1 is the name of the server control. It is not the name that ends up being rendered in the client. The two are different because asp.net needs to ensure the control hass a unique name. You could have any number of user
controls on a page, each with their own FileUpload1 which would end up conflicting.
It's easy to get the unique name of the control through the ClientID property of the control.
var documentid = document.getElementById('<%= FileUpload1.ClientID %>');
if(documentid != null)
{
alert('you selected the file: '+ documentid value);
}
I altered it a bit so that the element should end up being returned into a variable. The reason is you need to ensure that you find the control properly before accessing the value property. Don't expect this to work though.
Most browsers will not let you have access to this property as there is no reason for you to have access in scripting since the filename is only truly usefull on the server side.
Don't forget to mark useful responses as Answer if they helped you towards a solution.
Marked as answer by Mark - MSFT on Feb 19, 2013 05:28 AM
polachan
Member
29 Points
242 Posts
Microsoft JScript runtime error: Unable to get value of the property 'value': object is null or u...
Feb 05, 2013 10:08 PM|LINK
When I run the script the following error is occuring . Please help to fix it. My script as follows
<td style="height: 12px" dir="ltr"> <asp:FileUpload ID="FileUpload1" runat="server" Width="317px" onchange="FileUpload_OnChange(this,event);" /> </td><script language="javascript" type="text/javascript"> // <![CDATA[ function FileUpload_OnChange(sender, e) { alert('you selected the file: ' + document.getElementById('FileUpload1').value); } // ]]> </script> </asp:Content>markfitzme
Star
14411 Points
2226 Posts
Re: Microsoft JScript runtime error: Unable to get value of the property 'value': object is null ...
Feb 05, 2013 11:12 PM|LINK
The problem here is that the FileUpload1 is the name of the server control. It is not the name that ends up being rendered in the client. The two are different because asp.net needs to ensure the control hass a unique name. You could have any number of user controls on a page, each with their own FileUpload1 which would end up conflicting.
It's easy to get the unique name of the control through the ClientID property of the control.
var documentid = document.getElementById('<%= FileUpload1.ClientID %>');
if(documentid != null)
{
alert('you selected the file: ' + documentid value);
}
I altered it a bit so that the element should end up being returned into a variable. The reason is you need to ensure that you find the control properly before accessing the value property. Don't expect this to work though. Most browsers will not let you have access to this property as there is no reason for you to have access in scripting since the filename is only truly usefull on the server side.