How to Preserve Fileupload value in Asp.net ?

If you form has asp:Fileupload control along with other controls like dropdownlist, calendar.. you will be facing standard problem of Fileupload control losing (the file) value when users click upload first and dropdown next.

This problem can be solved by using Updatepanel.  (AJAX)

Place all the controls inside updatepanel and place the FileUpload control and Upload button outside the panel.

Any autopostbacks will be handled by AJAX and Full postback will be handled by Upload button. 

Example:

<asp:UpdatePanel ID=”UpdatePanel1″ runat=”server”>
   <ContentTemplate>
      <asp:DropDownList runat=”server” ID=”ddl” AutoPostBack=”true”>  </asp:DropDownList>
 <br/>
      <asp:Calendar ID=”calAsOfDate” runat=”server” DayNameFormat=”Shortest”> </asp:Calendar>
   </ContentTemplate>
</asp:UpdatePanel>

Fileupload placed outside the Panel

<asp:FileUpload Width=”600″ ID=”uldReport” runat=”server” /> <br/><br/>

<asp:button ID=”cmdCancel” runat=”server” UseSubmitBehavior=”false” CausesValidation=”false” Text=”Cancel” />&nbsp;&nbsp;

<asp:Button ID=”cmdUpload” runat=”server” Text=”Upload” OnClick=”cmdUpload_Click” />

Problem solved !!

5 comments

  1. Hi
    But my layout is different. I have a Fileupload control in a Formview and for the whole container i put a single update panel. So i cant remove the file upload control from update panel. So wat to do with this prob?
    Is there any way to prevent the file upload control to donot participate partial page rendering? I mean inside update panel..

    Thanks in advance
    Murali M
    .Net developer

  2. ur code only reserves the value but not the actual content! try to add selectedindexchanged event on ddl and do uldReport.FileName. You will get empty string!

  3. thank you mannnnnnnnnnn, I was trying to find a solution for this for a long time.. The mistake I made was placing the update panel into whole page.. thanks..

Leave a Reply

Your email address will not be published. Required fields are marked *

*