Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileUpload and UpdatePanel: ScriptManager.RegisterPostBackControl works the second time

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.

like image 930
VansFannel Avatar asked Feb 15 '10 07:02

VansFannel


2 Answers

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

like image 153
user326796 Avatar answered Sep 18 '22 10:09

user326796


Put the following in your form tag:

enctype="multipart/form-data"

like image 28
RepDetec Avatar answered Sep 21 '22 10:09

RepDetec