I have a asp.net 3.5 application with the following structure: masterpage (with update panel) nested masterpage content page multiple user controls (with file uploads) Obviously there is an issue with fileuploads inside update panels. The fix is to set a trigger. However, I cannot figure out how to set the trigger.
I've tried setting the trigger on Page_Load of the user control, which works when I debug, but when I actually click the button to upload the file it appears the trigger disappears from the update panel.
I've tried adding it first thing to the click event.
I've tried adding an update panel in the user control and setting all update panels to update conditionally. (I could set the trigger directly in the aspx this way, but it still did not work).
Here is the relevant html on my masterpage:
<ajax:ToolkitScriptManager ID="sm" runat="server" AsyncPostBackTimeout="600" /><asp:UpdatePanel ID="up" runat="server" RenderMode="Inline" UpdateMode="Conditional"><ContentTemplate><asp:contentplaceholder id="ContentPlaceHolder1" runat="server" /></ContentTemplate></asp:UpdatePanel>
Here is the relevant html on the nested masterpage (nothing really):
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server" />
Here is the relevant html on the content page:
<uc1:Observation ID="obGrasses" runat="server" TypeId="20" />
Here is the relevant html in the user control:
<asp:UpdatePanel ID="up" runat="server" RenderMode="Inline" UpdateMode="Conditional"><Triggers><asp:PostBackTrigger ControlID="btnUpload" /></Triggers><ContentTemplate><asp:Panel ID="pnlUpload" runat="server" Visible="false"><p><b>Add New Image:</b></p><p><asp:FileUpload ID="PhotoFile" runat="server" /></p><asp:Label ID="lblMsg" runat="server" Font-Bold="true" ForeColor="red" Font-Size="11px" /><p><asp:Button ID="btnUpload" runat="server" Text="Upload Image" /></p></asp:Panel></ContentTemplate></asp:UpdatePanel>
In my user control code behind I've tried to also bind it programatically to the masterpage update panel:
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click '-----Add Trigger Dim sm As AjaxControlToolkit.ToolkitScriptManager = DirectCast(Me.Page.Master.Master.FindControl("sm"), AjaxControlToolkit.ToolkitScriptManager) Dim mup As UpdatePanel = DirectCast(Me.Page.Master.Master.FindControl("up"), UpdatePanel) Dim t As New PostBackTrigger t.ControlID = btnUpload.ClientID mup.Triggers.Add(t) sm.RegisterPostBackControl(btnUpload) mup.Update() '--------------- End Sub
This code adds the trigger see my immediate window from the btnUpload_Click:
? DirectCast(Me.Page.Master.Master.FindControl("up"), UpdatePanel).Triggers(0).ToString "PostBack: ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_obGrasses_btnUpload"
This same result occurs in the Page_Load if I try it there, but the File Upload is still returning "Nothing" on the button click. So, for some reason the trigger isn't working.
When I try to put the code in on Page_Init I get the error:
A control with ID 'ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_obGrasses_btnUpload' could not be found for the trigger in UpdatePanel 'up'.
I've also separated the button code by utilizing the btnUpload_Command thinking maybe that would give time for the trigger to register in the Click event, but separating them didn't make a difference either.
So it seems like the trigger isn't triggering, maybe it's not getting set early enough in the life cycle? But no matter where I try to set the trigger, it shows up in debug when I try to access the FileUpload.PostedFile.
I also don't know why the approach of adding the second update panel in the user control and setting to conditional updating and setting the trigger directly in html is causing the masterpage update panel to update.
I wonder if there is a way to set this trigger directly using javascript somehow? Has anyone done this? Or ran into this issue?
I can't figure this out and I've tried everything I can think of.