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 thoughts on “How to Preserve Fileupload value in Asp.net ?

  • August 26, 2008 at 3:48 am
    Permalink

    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

    Reply
  • October 15, 2008 at 10:55 am
    Permalink

    Hi
    This solved my problem. I separated the control out of update panel and that fixed it.

    Reply
  • October 5, 2009 at 11:36 pm
    Permalink

    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!

    Reply
  • January 5, 2012 at 3:17 pm
    Permalink

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

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: