Search code examples
c#asp.netfile-uploadpostbackasyncpostbackerror

FileUpload control working on second click but not first attempt of saving posted file?


My Question

I managed to answer myself, however the same set of functionality has another problem. For some reason the first postback of the save event of the posted file hits the Ol' Object not set to an instance of an object error, but on the second attempt of uploading a file and firing my save event (converts to byte[] an stored as SQL Server BLOB) it does everything is supposed to do.

Same problem here

There is a good suggestion of using the AJAX AsyncUpload control however I am a firm believer of removing the cause and not treating the problem. I will continue down this route to best my understanding of asp.net etc.

Would there be a wizrd amongst you that could help me identify why I get "object ref not set to inst of obj" error on first postback but on second it works fine. Content page has a master page which wraps content page in an update panel. Not my decision to do this. There is also an update panel with postback triggers targeting my save event.

What are your thoughts people?


Solution

  • The problem (as seen here http://forums.asp.net/t/1060363.aspx) seems to be when you use the visibility property on the surrounding panel (as it seems you are from the linked question).

    The suggested workaround is to use CSS visibility instead so use this to make it invisible -

    <asp:Panel ID="pnlUpload" runat="server" class="workerDetailsPanelLeft" style="display:none">
    

    The explanation for this from the thread is

    If your container is set to invisible, the upload control is not actually rendered as HTML, causing the form's enctype not to be set to enctype="multipart/form-data", causing the file upload control not to post the selected file back to the server. The workaround is either to make sure the FileUpload control is rendered to HTML (by setting its style to display:none in stead of Visible=false), or by manually setting the enctype

    So another workaround would be to alter your form tag to this

    <form id="form1" enctype="multipart/form-data" runat="server">
    

    I think either one of those should solve your problem.