In this article I will explain why are the FileUpload control properties PostedFile is NULL and HasFile is false when FileUpload control is placed inside ASP.Net AJAX UpdatePanel.

This happens because FileUpload control does not work with partial PostBack which is done in UpdatePanel. FileUpload control requires a full PostBack.

Hence when you place FileUpload control in AJAX UpdatePanel and try to upload the file asynchronously using the PostedFile property is always NULL and the HasFile property is always false.

So the question arises How to use FileUpload Control in UpdatePanel? The answer is by just changing the Trigger of the upload button from AsyncPostBackTrigger to PostBackTrigger. This means that even if the FileUpload control is inside UpdatePanel still there will be a Full PostBack when upload button is clicked.

To test it out I developed a small application with a FileUpload control and two buttons

1. To upload the file asynchronously using AsyncPostBackTrigger with partial postback.

2. To upload the file synchronously using PostBackTrigger with full postback.

<asp:ScriptManager ID="ScriptManager1" runat="server" />

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

    <ContentTemplate>

        <asp:FileUpload ID="FileUpload1" runat="server" />

        <asp:Button ID="btnAsyncUpload" runat="server"

           Text="Async_Upload" OnClick = "Async_Upload_File" />

        <asp:Button ID="btnUpload" runat="server" Text="Upload"

           OnClick = "Upload_File" />               

    </ContentTemplate>

    <Triggers>

        <asp:AsyncPostBackTrigger ControlID = "btnAsyncUpload"

          EventName = "Click" />

        <asp:PostBackTrigger ControlID = "btnUpload" />

    </Triggers>

</asp:UpdatePanel>

 

Then I clicked both the upload buttons to see how the FileUpload control was behaving in the two scenarios.

 

Using AsyncPostBackTrigger

The below figure shows that when the FileUpload is does asynchronously using AsyncPostBackTrigger the HasFile property of the FileUpload control is false.

Why Posted File is NULL and HasFile is false when FileUpload is inside AJAX UpdatePanel in ASP.Net


Using PostBackTrigger

The below figure shows that when the FileUpload is does synchronously using PostBackTrigger the HasFile property of the FileUpload control is true.

Why Posted File is NULL and HasFile is false when FileUpload is inside AJAX UpdatePanel in ASP.Net