locked
Unable to modify sibling controls' properties in an AsyncFileUpload event handler RRS feed

  • Question

  • User-1409619497 posted

    I have an AsyncFileUpload, an ImageButton, and a TextBox wrapped into a .ascx user control, which contains an UpdatePanel itself holding the above to carry out partial update in a table cell. Simplified by taking out some layout code and it's straightforward enough:

        <form id="form1" enctype="multipart/form-data" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <uc1:ResourceCellControl runat="server" ID="ResourceCellControl" />
        </form>

    And the .ascx markup:

    <asp:UpdatePanel ID="UpdatePanelListViewCell" runat="server">
    
        <ContentTemplate>
    
               <asp:ImageButton ID="UploadImgBtn" runat="server" Height="40px" OnClick="UploadImgBtn_Click" Width="40px" />
    	   <asp:TextBox ID="TBURL" runat="server" Height="12px" Width="95%" Font-Size="X-Small" AutoPostBack="True" ClientIDMode="AutoID" OnTextChanged="TBURL_TextChanged"></asp:TextBox>
               <ajaxToolkit:AsyncFileUpload ID="AsyncFileUpload" runat="server" OnUploadedComplete="AsyncFileUpload_UploadedComplete" />
    
        </ContentTemplate>
    
        <Triggers>        
            <asp:AsyncPostBackTrigger ControlID="TBURL" EventName="TextChanged" />
            <asp:AsyncPostBackTrigger ControlID="UploadImgBtn" />
        </Triggers>
    
    </asp:UpdatePanel>

    The whole idea is when the upload is complete, show a new filename renamed from the original uploaded one in the Textbox and replace the src of ImageButton with the location of the uploaded file (with another intent of clicking it to open a new window to show the uploaded image in full), so I tried to utilize the OnUploadedComplete event handler:

        protected void AsyncFileUpload_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
        {       
    
            var afu = sender as AsyncFileUpload;
            if (afu.HasFile)
            {
                TextBox tb = afu.Parent.FindControl("TBURL") as TextBox;
    
                string fileExtension = System.IO.Path.GetExtension(afu.FileName);
                int found = validExts.Where(result => result == fileExtension).Count();
    
                if (found == 0)
                {
                    tb.Text = "Valid file types: " + validExts;
                }
                else
                {
                    tb.Text = afu.FileName;				
                }
    
                UploadImgBtn.AlternateText = "Hello";
    
                //UpdatePanelListViewCell.Update();
            }
        }

    However, with the above experimental code, I found the properties from the sibling controls,  such as TextBox.Text and the UploadImgBtn.AlternateText, cannot be modified, despite both of them can be accessed and their properties can be modified when the debugger is tracing through them, after that their values are still not changed on the page. I also tried to add a Click event handler to ImageButton to change its alternate text, it worked, so I guess there are some special mechanism or issues on AsyncFileUpload to prevent such behaviors. Anyone knows how to modify sibling controls' properties in an AsyncFileUpload event handler or any workarounds? Thanks.

    Wednesday, November 11, 2015 1:26 PM

All replies

  • User61956409 posted

    Hi AlexTs,

    AlexTs

    //UpdatePanelListViewCell.Update();

    UpdatePanel Update() Method could be used to update the UpdatePanel control's content in the browser. I’d like to know whether content could be updated if you don’t comment out the above code.

    Best Regards,

    Fei Han

    Wednesday, November 11, 2015 9:06 PM
  • User-1409619497 posted

    Hi Fei,

    Thanks for your first reply. No luck in using that Update() method was why I commented it out. I tried it with UpdateMode=Conditional but the content update never happened, same as right now I run the UpdatePanel with UpdateMode=Always, where the Update() has to be commented out otherwise a System.InvalidOperationException will be thrown. Any workarounds available we can use?

    p.s., I also feel there might be a solution combining the OnClientUploadComplete and UploadedComplete event handlers both on client and server sides, but this would definitely involve more sophisticated communication between client code and server code, which would be tedious and unlike the concise style of UpdatePanel to control everything on server side without a line of JS code. Much appreciated if there's a more elegant solution to this.

    BR,

    Alex

    Wednesday, November 11, 2015 10:08 PM