File Upload Control is not working in Update Panel

Last post 12-21-2009 5:17 AM by alainhol. 22 replies.

Sort Posts:

  • File Upload Control is not working in Update Panel

    04-30-2007, 11:49 PM

    hi friend...

    i am using update panel... in one of my aspx page (ASP.NET 2.0)

    there is a file upload control in update panel which is used to upload company logo on server..

    when i remove the update panel.. than file upload is working properly..

    but when i again place an update panel then file upload control is not working and its giving me error.. in like

    If String.IsNullOrEmpty(fldLogo.PostedFile.FileName.ToString) Then                 

    as object reference not set to an instance of an object...

     

    can any body help me ?

    any idea ?? 

     

    नमस्ते,
    [KaushaL] || BloG || MS MVP

    "I would love to change the world, but they won’t give me the source code"


    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.
  • Re: File Upload Control is not working in Update Panel

    04-30-2007, 11:59 PM
    Answer
    • Contributor
      2,898 point Contributor
    • jminond
    • Member since 07-21-2003, 6:33 PM
    • New York
    • Posts 608

    This problem is somewhat well documented, the update panel is listed as not working with certain controls.

    File upload, and tree view being 2 of the biggies.

    IN any case, one solution is available on code project:

    http://www.codeproject.com/useritems/simpleajaxupload.asp

    if you look around the forums and google, you can read more about the problem if your interested, but the code project solution should get you started.

     

    Jonathan Minond
    http://www.Jonavi.com
    http://www.jonavi.com/Default.aspx?pageID=21
    http://RainbowBeta.com
    http://community.rainbowportal.net/blogs/jonathans_rainbow_blog/default.aspx
    http://dotnetslackers.com/community/blogs/jminond/default.aspx
  • Re: File Upload Control is not working in Update Panel

    05-01-2007, 12:26 AM
    Answer

    i dont know about this thing..

    well i set trigger to the button i want to do asynchronous postback...

    and its working... with full postback to fileupload control (upload button click)

    and u provide me some link for the document you have specified inyour post .. which controls don't work with update panel ??

    thanks 

    नमस्ते,
    [KaushaL] || BloG || MS MVP

    "I would love to change the world, but they won’t give me the source code"


    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.
  • Re: File Upload Control is not working in Update Panel

    05-01-2007, 8:37 AM
    Answer
    • Contributor
      2,898 point Contributor
    • jminond
    • Member since 07-21-2003, 6:33 PM
    • New York
    • Posts 608

    From the online documentation:

    http://ajax.asp.net/docs/overview/UpdatePanelOverview.aspx

     

    Controls that Are Not Compatible with UpdatePanel Controls

    The following ASP.NET controls are not compatible with partial-page updates, and are therefore not supported inside an UpdatePanel control:

    Controls that are incompatible with partial-page rendering can still be used on a page outside UpdatePanel controls. Additionally, in some cases you can use the controls in a specific way to make them compatible with partial-page updates. For example, you can use the Login, ChangePassword, or PasswordRecovery controls inside an UpdatePanel control if you can convert their contents to templates. (If you are using Visual Studio, in Design view you can convert the controls by using smart-tag menu commands such as Convert to Template or Customize Create User Step.) When you convert these controls into editable templates, the validation controls that are used in the control are defined declaratively by using markup in the page. To make the validators compatible with an UpdatePanel control, set the EnableClientScript property of the validators to false. This disables the client script that would ordinarily be used to perform validation in the browser. As a result, during an asynchronous postback, the validators perform validation on the server. However, because only the content of the UpdatePanel is refreshed, the validators can provide the kind of immediate feedback that is ordinarily provided by client script.

    To use a FileUpload control inside an UpdatePanel control, set the postback control that submits the file to be a PostBackTrigger control for the panel.

    All other controls work inside UpdatePanel controls. However, in some circumstances, a control might not work as expected inside an UpdatePanel control. These circumstances include the following:

    • Registering script by calling registration methods of the ClientScriptManager control.

    • Rendering script or markup directly during control rendering, such as by calling the Write(String) method.

    If the control calls script registration methods of the ClientScriptManager control, you could use corresponding script registration methods of the ScriptManager control instead. In that case, the control can work inside an UpdatePanel control.

    Jonathan Minond
    http://www.Jonavi.com
    http://www.jonavi.com/Default.aspx?pageID=21
    http://RainbowBeta.com
    http://community.rainbowportal.net/blogs/jonathans_rainbow_blog/default.aspx
    http://dotnetslackers.com/community/blogs/jminond/default.aspx
  • Re: File Upload Control is not working in Update Panel

    05-01-2007, 12:26 PM
    • Member
      20 point Member
    • hooked82
    • Member since 04-23-2007, 4:28 PM
    • Posts 12
    I am having the same problem but it seems that it should work as I don't have a FileUpload control inside the Update Panel, I'm just trying to update a label when the Upload button is clicked.  I guess it has to do with since the button_click is getting info from the FileUpload control, it is unable to do so, is this correct?
  • Re: File Upload Control is not working in Update Panel

    05-01-2007, 12:49 PM
    Answer
    • Contributor
      2,898 point Contributor
    • jminond
    • Member since 07-21-2003, 6:33 PM
    • New York
    • Posts 608

    From what you are saying you have part of a page relying on a full postback, and part on partial updates.

    When the submit is clicked for the upload, and the page posts back, you will need to use code behind to set the label inside the update panel, using something like

    if(IsPostBack) {
       .....
       // Handle Upload File Code
       ....
        txtLabelContro.Text = "Whatever string you want";
    }

     If you do not set the label on the postback, it will never get the value, as the update panel wont know what you want from it. The update panel is a graet control, but it can't jsut automate everything.

    So, since your uplaod control is outside the panel, the panel is not even aware of the upload control, file, or any of that.

    Jonathan Minond
    http://www.Jonavi.com
    http://www.jonavi.com/Default.aspx?pageID=21
    http://RainbowBeta.com
    http://community.rainbowportal.net/blogs/jonathans_rainbow_blog/default.aspx
    http://dotnetslackers.com/community/blogs/jminond/default.aspx
  • Re: File Upload Control is not working in Update Panel

    05-01-2007, 5:53 PM
    • Member
      20 point Member
    • hooked82
    • Member since 04-23-2007, 4:28 PM
    • Posts 12

    Yeah, I'm setting the text of the label in the code behind.  Here is more of what I'm doing.

    When the user selects a file to upload from the browse button, they will hit the Upload button(Actually named submit) to check to see if the file passes certain criteria.  If it does, then it will store it to be saved later when a final button is clicked to submit form.  What is actually messing up is on Button_Click I have an if statement to see if the fileupload has a file, like so:

    if (FileUpload1.HasFile)

    {

        //Do Stuff

    Well it is skipping right over this and going to the else statement.  The only thing in my update panel is my label.  Any ideas?  I can post my full code if needed, thanks!! 

  • Re: File Upload Control is not working in Update Panel

    05-02-2007, 1:43 AM

    FileUpload controls when they are used to upload files as part of an asynchronous postback.

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: File Upload Control is not working in Update Panel

    05-02-2007, 8:33 AM
    • Contributor
      2,898 point Contributor
    • jminond
    • Member since 07-21-2003, 6:33 PM
    • New York
    • Posts 608

    If the label is the only thing in the update panel, what do you need it for?

    The upload button will cause the post back for you anyway, unless you follow one of the other methods mentioned.

    So if you have the postback, and are setting the label then, remove the update panel, and use standard IsPostBack logic.

    Can you post your whole ASPX and CodeBehind Pages? So we can see what is going on?

    Jonathan Minond
    http://www.Jonavi.com
    http://www.jonavi.com/Default.aspx?pageID=21
    http://RainbowBeta.com
    http://community.rainbowportal.net/blogs/jonathans_rainbow_blog/default.aspx
    http://dotnetslackers.com/community/blogs/jminond/default.aspx
  • Re: File Upload Control is not working in Update Panel

    05-02-2007, 2:46 PM
    • Member
      20 point Member
    • hooked82
    • Member since 04-23-2007, 4:28 PM
    • Posts 12

    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.";
                }
                else
                {
                    SubmitButtonLabel2.Text = "File type not allowed.  Please choose another.";
                }
            }
            else
            {
                SubmitButtonLabel.Text = "Please select a file.";
            }

     

    Page: 

     <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <div>
                <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">
                    <ContentTemplate>
                        <asp:Label ID="SubmitButtonLabel" runat="Server" />
                    </ContentTemplate>
                    <Triggers>
                        <asp:PostBackTrigger ControlID="SubmitButton" />
                    </Triggers>
                </asp:UpdatePanel>
                
            </div>
        </form>
    </body>

  • Re: File Upload Control is not working in Update Panel

    05-02-2007, 3:07 PM
    • Contributor
      2,898 point Contributor
    • jminond
    • Member since 07-21-2003, 6:33 PM
    • New York
    • Posts 608

    You are missing the point a little.

    The triggere in your panel, is causing the submit button to post the panel, not the file.

    So the post back, does not have a file, that is why your property HasFile is empty.

    get rid of the trigger, and the update panel, you do not need it for your code.

    You CAN NOT acutally determine the file size, etc... without posting back, unless you do some other methods, described in links we gave you already.

    The solution you want, is not doable. You can not have the asynch post back and the upload work together, without doing something like the details in the links we provided.

    You have to accept that the update panel and the upload control are doing two different things, and will not work together without other stuff invovled.

     

    Jonathan Minond
    http://www.Jonavi.com
    http://www.jonavi.com/Default.aspx?pageID=21
    http://RainbowBeta.com
    http://community.rainbowportal.net/blogs/jonathans_rainbow_blog/default.aspx
    http://dotnetslackers.com/community/blogs/jminond/default.aspx
  • Re: File Upload Control is not working in Update Panel

    05-02-2007, 3:48 PM
    • Member
      20 point Member
    • hooked82
    • Member since 04-23-2007, 4:28 PM
    • Posts 12
    Well that sucks.  All I need to do with the async button is let the user know if the selected file passes and then on the final button click it will post back and upload file without using an UpdatePanel.  Thanks for your help with this jminond!!
  • Re: File Upload Control is not working in Update Panel

    05-02-2007, 4:11 PM
    • Contributor
      2,898 point Contributor
    • jminond
    • Member since 07-21-2003, 6:33 PM
    • New York
    • Posts 608

    To validate the extension, you can use some javascript.
    <input type="file" name="filesent" onSubmit="return CheckUpload()" />
    function CheckUpload()
    {
      var filename = document.form1.filesent.value;

      /// add javascript to check the filename extension and approve or not.
      ///
    }

    To validate the size, you will have to post it and check the file length on the server. Unless you use some sort of custom activex or java thing, that lets you handle the files on the client side.

    You can see lots of ideas here:

    http://www.google.com/search?hl=en&q=javascript+file+upload+control+check+extension

    Commercial Product: http://encodable.com/filechucker/   ($20)

     

     

    Jonathan Minond
    http://www.Jonavi.com
    http://www.jonavi.com/Default.aspx?pageID=21
    http://RainbowBeta.com
    http://community.rainbowportal.net/blogs/jonathans_rainbow_blog/default.aspx
    http://dotnetslackers.com/community/blogs/jminond/default.aspx
  • Re: File Upload Control is not working in Update Panel

    05-02-2007, 4:28 PM
    • Member
      20 point Member
    • hooked82
    • Member since 04-23-2007, 4:28 PM
    • Posts 12
    Thanks for the help and the link, I will check in to that!
  • Re: File Upload Control is not working in Update Panel

    06-18-2007, 5:18 AM
    • Member
      17 point Member
    • mieliespoor
    • Member since 01-25-2007, 11:51 AM
    • Posts 25

    I have read through all of this, but I still can't get my fileupload to work.

    Before I have read this, I came to the conclusion that the fileupload don't work in an update panel so I have taken it out of the update panel hoping this will work...still not working. After much struggling, I have seen that as soon as I take my scriptmanager off of the page, the fileupload works. Is this a known error?

    Can I do something to fix this or should I think of something else to get files uploaded to my page?

    I really need the scriptmanager on the page, because I really need a updatepanel. But I need a file upload control just as bad!!

     Any ideas?

Page 1 of 2 (23 items) 1 2 Next >