Reputation: 949
I am using asp.net file upload
<asp:FileUpload ID="ImageUploader" runat="server"/>
how can i detect that asp:FileUpload has a file using Jquery?
I am doing this
$("#ctl00_MainContentPlaceHolder_UCUpdOrgProfile1_ImageUploader").change(function (e) {
alert("hello")
});
But i dont know either file is selected or not.
Upvotes: 0
Views: 4533
Reputation: 340
Valid answers, or since ASP 4+ just set ClientIDMode as a page property:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" ClientIDMode="Static" %>
The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains. - reference
ie. this will force ASP to obey the ID as declared and not generate the "ctl00_" prefix or any other. This is awesome as it is applied to the whole page and all asp controls contained on it, no extra code, no hacks just one awesome property.
So apply the same logic as @Microsoft_DN solution, but use static ID's.
Upvotes: 1
Reputation: 10030
check -
if (document.getElementById('<%= ImageUploader.ClientID %>').files.length === 0)
{
// File upload do not have file
}
else {
// File upload has file
}
Upvotes: 2