0

I have a AjaxFileUpload control in a usercontrol which is loaded dynamicaly on Postback, the problem here is, once the file is uploaded IsPostBack is false due to which the usercontrol is not loaded, causing the OnUploadCompleteAll event to not be triggered.

I found out that AjaxFileUpload control has its own postback property AjaxFileUpload.IsInFileUploadPostBack, how do i access this property from my main WebForm1.aspx page?

When an event is triggered from AjaxFileUpload i would like to check IsInFileUploadPostBack on page load of WebForm1.aspx and then load the usercontrol.

here is the code.

WebForm1.aspx

<form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:Button ID="Button1" runat="server" Text="Load Control" OnClick="Button1_Click" />
        <asp:PlaceHolder ID="Placeholder1" runat="server" />

    </form>

Codebehind

protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                loadcontrol();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

       private void loadcontrol()
        {
            this.Placeholder1.Controls.Clear();
            var _controls = LoadControl("~/WebUserControl1.ascx");
            this.Placeholder1.Controls.Add(_controls);
        }

WebUserControl1.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="AjaxFileUpload_Test.WebUserControl1" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>

<div id="div1">

        <ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" UseAbsoluteHandlerPath="false" OnUploadCompleteAll="AjaxFileUpload1_UploadCompleteAll" OnUploadStart="AjaxFileUpload1_UploadStart" runat="server" />
</div>
Telson Alva
  • 842
  • 6
  • 22
  • 37
  • Potential duplicate of: http://stackoverflow.com/questions/14665667/ajaxfileupload-postback-false – Paul Swetz Jun 14 '16 at 13:18
  • There are only so many ways to say "it cant work as described" This part is impossible "IsInFileUploadPostBack on page load of WebForm1.aspx and then load the usercontrol." You cannot check a property of something that has not been loaded. – Paul Swetz Jun 14 '16 at 13:23
  • Understood ... lets say i want to check the property in loadcontrol() after the control has been loaded, how do i do that? – Telson Alva Jun 14 '16 at 13:25
  • Never mind, i answered that myself, also found the cause to my problem – Telson Alva Jun 14 '16 at 13:27

2 Answers2

0

Is there any specific reason you are loading the usercontrol from codebehind? An ajax control does not create an actual postback which is why that is always false. For what you seem to want you should use a NON-ajax file upload.

Paul Swetz
  • 2,234
  • 1
  • 11
  • 28
  • i have many options on the main page, based on those options i load the respective user controls. AjaxFileUpload control has a property it sets for post back, how can i access that property from my main page? – Telson Alva Jun 14 '16 at 13:05
  • The ajax control post to a hidden frame so you will never get the normal IsPostBack part to trigger which in turn prevents you from being able to load the control correctly in page load which is what you are seeing. Because of that you won't be able to access the control or IsInFileUploadPostBack. What you can try to do is ALWAYS load that control on the page but keep it hidden that way you can always be assured the control is loaded, but only display it when needed using some client javascript, then you should be able to check the IsInFileUploadPostBack property. – Paul Swetz Jun 14 '16 at 13:16
  • Also I think if you are going to dynamic load the control you need to do it earlier in the page lifecycle so it gets the viewstate information correctly, page load is too late (I believe, its been awhile). – Paul Swetz Jun 14 '16 at 13:17
  • Thanks ! once the controls are loaded (even dynamically) cant i use 'FindControl' ? I always seem to be getting null though. – Telson Alva Jun 14 '16 at 13:22
  • The control has to be loaded earlier in the page lifecycle for it to be present in the page control collection which is what FindControl is looking at. Instead of page load try adding your controls in Page_Init, then they should get viewstate correctly (if loaded before last postback) and show up in the FindControl correctly. You should generally load any dynamic controls in the init not the load methods. – Paul Swetz Jun 14 '16 at 13:27
  • Thanks for that suggestion, i moved the controls to Page_Init() , i also have a workaround which i shall post as an answer, let me know your thoughts – Telson Alva Jun 14 '16 at 15:57
0

Got it working !!

Since i have to load the controls only then will i be able to access AjaxFileUpload.IsInFileUploadPostBack property, i decided to take an unorthodox approach.

Added below code to Page_Init

//to load the controls if AsyncFileUpload causes a postback
//the URL should contain the ID of the control in its context query string
    if (Request.HttpMethod.Contains("POST") && Request.Url.AbsoluteUri.Contains("AjaxFileUpload1"))
          loadcontrol();
Telson Alva
  • 842
  • 6
  • 22
  • 37