0

This is not just another FileUpload + UpdatePanel question.

I have, as stated in the many similar posts, an UpdatePanel and a FileUpload control on my form. I also have a PostBackTrigger set up for my upload button. It works. The catch is it never does work on first time click. That is:

I click on browse, select my file, press upload. Nothing happens (fupld.HasFile = false);

I click on browse again, select any file (the same or another), press upload and it works fine.

<asp:UpdatePanel ID="upGeneral" runat="server" >
   <ContentTemplate>
       ...
       <table id="tabPage10" runat="server" visible="false" width="100%" >
          <tr>
              ...
              <td>
                  <asp:FileUpload ID="fupld" runat="server" Width="80%" />
                  <asp:ImageButton ID="ibtnUpld" runat="server" onclick="ibtnUpld_Click" />
              <td>
              ...
          <tr>
       ...
   </ContentTemplate>
   <Triggers>
      <asp:PostBackTrigger ControlID="ibtnUpld" />
   </Triggers>
</asp:UpdatePanel>

I've looked around for answers, but this is some really weird behaviour. No luck so far.

Any ideas?

Thanks

Ricardo Appleton
  • 679
  • 10
  • 22

3 Answers3

4

Well, the FileUpload control is designed to be used only in postback scenarios and not in asynchronous postback scenarios during partial-page rendering.

http://msdn.microsoft.com/en-us/ysf0192b#using_the_FileUpload_Control_with_the_UpdatePanel_control

You could use the AsyncFileUpload control instead from the AjaxControlToolkit.

<asp:AsyncFileUpload runat="server" ID="asyncFileUpload" Width="400px" ThrobberID="imageThrobber"
   OnClientUploadStarted="uploadStarted" OnClientUploadError="uploadError"
   ClientIDMode="AutoID" PersistFile="true" PersistedStoreType="Session" />

code behind:

     if (asyncFileUpload.HasFile)
     {
        string fullPath = GetPath(asyncFileUpload.FileName);
        asyncFileUpload.SaveAs(fullPath);
     }

I've never had any problems with it.

Ed B
  • 6,028
  • 3
  • 26
  • 35
1

With Post back triggers I had to use the below line of code in code behind:

Page.Form.Enctype = "multipart/form-data";

And this works perfectly fine.

Thanks to the solution in the link - (http://patelshailesh.com/index.php/file-upload-control-fails-first-time-then-works-on-subsequent-submits).

slfan
  • 8,950
  • 115
  • 65
  • 78
Subham Das
  • 11
  • 1
0

Change

Visible ="false"

to

style="display:none"

and change it from code behind.
If you set Visible="false, control is not actually rendered as HTML.
To rendered it as HTML, use style = "display:none" instead of Visible="false"

Singh T
  • 161
  • 1
  • 5