FileUpload and UpdatePanel: ScriptManager.RegisterPostBackControl works a second time

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

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

One user control that I used in the right pane has a FileUpload control and a button for saving this file to the server. Ascx code to save control:

<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 am doing a full postback to upload the file to the server and save it to the database. But I always get False on FileUpload.HasFile.

The problem I am is the correct UpdatePanel. I need it to dynamically load user controls. This panel has three updates for loading the three user controls that I use.

Perhaps I can use Async File Uploader or remove the necessary update panel and do a full postback to dynamically load controls.

Any tips?
UPDATE:

RegisterPostBackControl working ... the second time I click the "Save" button. The first time FileUpload.HasFile is FALSE, and the second time is TRUE.

Second update
On the first click, I also check ScriptManager.IsInAsyncPostBack and FALSE. I do not understand anything!

Why?

Code for loading the user control for the first time and for each postback:

 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); } 

Thanks.

+7
c # file-upload asp.net-ajax updatepanel
source share
6 answers

For me, this solution worked:

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

On the first callback, the enctype attribute is missing.

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

+26
source share

Put the following in your form tag:

ENCTYPE = "Multipart / Form Data"

+3
source share

Filling out a download requires a full recording, and if you use the update panel, it performs a partial recording. Thus, FileUpload alone will not work. You will have to handle it differently by calling RegisterPostBackControl .

See this link for more details → http://geekswithblogs.net/mmintoff/archive/2009/04/01/fileupload-within-updatepanel.aspx

+1
source share

In response to the second update

I had this exact problem, and I believe that this is because you are adding a dynamic control to your page inside the UpdatePanel.

There may be a better way to do this, since I'm certainly not an ASP.NET expert, but I was able to solve this problem myself by adding a control to the div that was hidden using CSS-before - it was necessary, and then automatically increase the dummy control identifier so that there are no conflicts, and I can continue to add as much as I want. Thus, when starting UpdatePanel, it fully recognizes the controls and their contents, with the exception of hidden ones, of course.

I use XSL to convert my XML to a dynamic page with ASP.NET controls, so I did the following:

 <div class="hide"> <asp:FileUpload CssClass="upload" identity="addVersion_{@id}_{count(draft) + 1}" runat="server" /> <asp:Button Text="Add File" identity="addVersionBtn_{@id}_{count(draft) + 1}" fileControlIdentity="addVersion_{@id}_{count(draft) + 1}" action="addVersion" runat="server" /> </div> 

And then in the code:

 Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init //Code to generate the data (stripped out because it is generated in a different manner than the original poster) //Add events for all of the new-found controls depending on their type recursiveAddEvents(nameOfPlaceHolder.Controls) End Sub //Add events for all of the new-found controls depending on their type Sub recursiveAddEvents(ByRef controls As ControlCollection) For Each con As Control In controls If con.Controls.Count > 0 Then recursiveAddEvents(con.Controls) End If //Try to cast the control to different data types Dim btn As Button = TryCast(con, Button) Dim file As FileUpload = TryCast(con, FileUpload) //Test to see which type the control was and apply the actions to it If Not btn Is Nothing Then //Assign the correct click events If btn.Attributes.Item("action") = "addVersion" Then AddHandler btn.Click, AddressOf addDraftVersion btn.ID = btn.Attributes.Item("identity") //Register the control with the ScriptManager ScriptManager.GetCurrent(Page).RegisterPostBackControl(btn) End If ElseIf Not file Is Nothing Then //Assign the correct click events file.ID = file.Attributes.Item("identity") End If Next End Sub Protected Sub addDraftVersion(ByVal sender As Button, ByVal e As EventArgs) Dim fileName as String = sender.Attributes("title").Replace(" ", "_") & "_D" & info("draftID") & "_V" & info("versionID") Dim inputControl As FileUpload = TryCast(trackPH.FindControl(sender.Attributes("fileControlIdentity")), FileUpload) If inputControl Is Nothing Then Exit Sub End If //Do whatever need to be done //Trigger UpdatePanel(s) nameOfUpdatePanel.Update() End Sub 

I deleted a lot of code, but it should still get a general idea :)

+1
source share

I had to combine both tips here. I use UpdatePanel to dynamically load UserControl, and FileUpload in UserControl. I had to:

  • RegisterPostBackControl in User Control Page_Load

  • add enctype = "multipart / form-data" to the page form element containing the update panel, code Page.Form.Attributes.Add ("enctype", "multipart / form-data"); didn't work for me, it should have been in aspx

Hope this helps.

+1
source share

This worked for me only when I put it in Page_Init

 Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init Page.Form.Attributes.Add("enctype", "multipart/form-data") End Sub 
0
source share

All Articles