FileUpload and UpdatePanel: ScriptManager.RegisterPostBackControl works the second time

VansFannel picture VansFannel · Feb 15, 2010 · Viewed 29k times · Source

I'm developing an ASP.NET application with C# and Visual Studio 2008 SP1. I'm using WebForms.

I have an ASPX page with two UpdatePanels, one on the left that holds a TreeView and other on the right where I load dynamically user controls.

One user control, that I used on right panel, has a FileUpload control and a button to save that file on server. The ascx code to save control is:

<asp:UpdatePanel ID="UpdatePanelBotons" runat="server" RenderMode="Inline" 
    UpdateMode="Conditional">
<ContentTemplate>
    <asp:Button ID="Save" runat="server" Text="Guardar" 
                onclick="Save_Click" CssClass="button" />
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="Save" />
    </Triggers>
</asp:UpdatePanel>

I make a full postback to upload the file to the server and save it to database. But I always getting False on FileUpload.HasFile.

I problem is the right UpdatePanel. I need it to load dynamically the user controls. This panel has three UpdatePanels to load the three user controls that I use.

Maybe I can use an Async File Uploader or delete the right Update Panel and do a full postback to load controls dynamically.

Any advice?
UPDATE:

RegisterPostBackControl works... the second time I click on save button. First time FileUpload.HasFile is FALSE, and second time is TRUE.

Second Update
On first click I also check ScriptManager.IsInAsyncPostBack and is FALSE. I don't understand ANYTHING!!

Why?

The code to load user control first time, and on each postback is:

DynamicControls.CreateDestination ud =
            this.LoadControl(ucUrl) as DynamicControls.CreateDestination;
if (ud != null)
{
    Button save = ud.FindControl("Save") as Button;
    if (save != null) 
        ScriptManager1.RegisterPostBackControl(save);
    PanelDestination.Controls.Add(ud);
}

Thank you.

Answer

user326796 picture user326796 · Apr 27, 2010

For me this solution worked:

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

The enctype attribute is missing on the first postback.

http://adamnoffie.blogspot.com/2007/10/file-upload-not-working-on-first.html