FileUpload is not working within update panel

So what I am trying to do is, have a user select a file to upload. Since I am only going to accept images, I will test the extension. I also want to limit the file size to under 2mb, so I will test that(haven’t implemented in code yet). If the file they have selected passes, then I want the label to say “File Accepted”, and store the file upload information for a later button click. This will happen once the user has finished filling out the rest of the form. Eventually, I will put an UpdateProgress control on the page while it is checking if the file is allowed. I would rather not have it post back for this, so if I can get it to work, that would be great. BTW, this will all work fine if I take the label out of the update panel.

What happens when I run this, is it will go to the else statement of the first if and return “Please select a file.” Meaning that FileUpload1.HasFile is returning false. The only reason I can see that this is happening is because the UpdatePanel can not access that info from the FileUpload control?

Code Behind:

    Label SubmitButtonLabel2= (Label)UpdatePanel1.FindControl("SubmitButtonLabel");
    if (FileUpload1.HasFile)
        string[] fileName = FileUpload1.FileName.Split('.');
        if ((fileName[fileName.Length - 1] == "jpg") ||
            (fileName[fileName.Length - 1] == "gif") ||
            (fileName[fileName.Length - 1] == "bmp") ||
            (fileName[fileName.Length - 1] == "jpeg") ||
            (fileName[fileName.Length - 1] == "png"))
            SubmitButtonLabel2.Text = "File Accepted.";
            SubmitButtonLabel2.Text = "File type not allowed.  Please choose another.";
        SubmitButtonLabel.Text = "Please select a file.";


<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="SubmitButton" runat="server" Text="Submit File" OnClick=SubmitButton_Click />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="always">
                <asp:Label ID="SubmitButtonLabel" runat="Server" />
                <asp:PostBackTrigger ControlID="SubmitButton" />


No need to do anything you just need to add multipart data attribute to your form.

Page.Form.Attributes.Add("enctype", "multipart/form-data");

See the following link for more details.

  <asp:PostBackTrigger ControlID="YourControlID" />

Add the trigger for the UpdatePanel and give the ControlID. In case you are using TabContainer, use the ID of the tab container.

Add this line in your page_load


If you use FileUpload control in Update panel you have to set PostbackTrigger for the button you write the code to save the upload file.

Now Following code I have btnSave button for save the file in the upload folder. So I set the postbacktrigger for it.

            <asp:PostBackTrigger ControlID="btnSave" />

Hope this will help you.

Default FileUpload control will never work with UpdatePanel. You need special AsyncFileUpload control as defined in AjaxControl Toolkit. This

alt text

 <ajaxToolkit:AsyncFileUpload OnClientUploadError="uploadError"
     OnClientUploadComplete="uploadComplete" runat="server"
     ID="AsyncFileUpload1" Width="400px" UploaderStyle="Modern"
     UploadingBackColor="#CCFFFF" ThrobberID="myThrobber" /> 

Don’t forget to change the type of the form, to allow file uploads (enctype or something like that, i’m not in front of Visual Studio so can’t be that precise.)

I had the same problem.

Make the the button to upload the file as the trigger of the Upload panel
Something like this,

 <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<asp:Panel ID="pnlUploadImage" runat="server">
 <asp:FileUpload ID="fuldImage" runat="server"></asp:FileUpload>

       <asp:LinkButton ID="lnkbUpload" runat="server" onclick="lnkbUpload_Click">Add</asp:LinkButton>

<asp:PostBackTrigger   ControlID="lnkbUpload"/></Triggers>

I got it working after using two of the solutions posted here.

I had to add both

Page.Form.Attributes.Add("enctype", "multipart/form-data");

on Page_Load as well as

    <asp:PostBackTrigger ControlID="btnUpload" />

on the markup for the update panel.

Page.Form.Attributes.Add("enctype", "multipart/form-data");

Doing this will solve your problem.

Refer to this article.

You’re answer can be found here

It is basically disallowed by default because of javascript and browser security reasons. But this is a workaround.

My guess would be that the HasFile will only be filled when the post is already done, not before that.

You might want to check if the FileUpload1.FileName is already filled before the post is done, but I kinda doubt that.

Leave a Comment