Search code examples
c#asp.netrepeaterasyncfileupload

Handling AsyncFileUpload's UploadComplete event inside repeater


I have a repeater that among other controls has an AsyncFileUpload and an error label all embedded inside a panel (regular, not update panel). In AFU's UploadComplete event I need to access the panel and the label; I can access the AFU itself using "sender" argument:

<asp:Repeater runat="server" ID="rpt1" ClientIDMode="Static" OnItemDataBound="rptQuestions_ItemDataBound">
    <ItemTemplate>
        < other controls>
        <asp:Panel runat="server" ID="pnlFU" clientidmode="static">
            <ajaxToolkit:AsyncFileUpload runat="server"
                ID="fuAttchedDocs" 
                clientidmode="static"
                ThrobberID="myThrobber"
                UploaderStyle="Traditional"
                OnClientUploadComplete="onClientUploadComplete"
                OnUploadedComplete="fuAttchedDocs_UploadedComplete"
                OnUploadedFileError="fuAttchedDocs_UploadedFileError" />
            <asp:Label runat="server" ID="lblError" clientidmode="static" Text="" CssClass="field-validation-error" Style="display: none" />
        </asp:Panel>
    </ItemTemplate>
</asp:Repeater>


protected void fuAttchedDocs_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
{
    AsyncFileUpload fuAttchedDocs = (AsyncFileUpload)sender;

    if (fuAttchedDocs.HasFile)
    {
        // How do I access these?

        lblError.Style["display"] = "none";
        ....
        pnlFU.Style["display"] = "block";
    }
}

How do I make sure I am accessing the correct panel and label inside the repeater?

Also, when "Submit" button, located outside repeater, is clicked I am using the following to make sure all files are uploaded at once and call a js function "sendResponse()" that does a postback to deal with all the repeater items.

<button type="submit" class="btn btn-primary btn-md" onclick="javascript:document.forms[0].encoding = 'multipart/form-data';sendResponse();">Submit Response</button>

Does this seem correct? I can't test it until I figure out accessing controls inside repeater but thought I check with you if it makes sense or not.


Solution

  • I'm unfamiliar with the AsynFileUpload tool, but I can show you how, in general, to access the Label in the same panel as the sender control.

    I set up an example page with roughly the same structure:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="TestRepeater.Test" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
    
        <form runat="server">
    
            <asp:Repeater ID="repeater" runat="server">
    
                <ItemTemplate>
    
                    <asp:Panel ID="ThePanel" runat="server">
    
                        <asp:TextBox ID="TheTextBox" OnTextChanged="TextBox_TextChanged" runat="server"></asp:TextBox>
    
                        <asp:Label ID="TheLabel" runat="server"></asp:Label>
    
                    </asp:Panel>
    
                </ItemTemplate>
    
            </asp:Repeater>
    
            <input type="submit" />
    
        </form>
    
    </body>
    </html>
    

    Here is the code-behind:

    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace TestRepeater
    {
        public partial class Test : Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    // Force the creation of three repeater items.
                    repeater.DataSource = new List<string>() { "", "", "" };
                    repeater.DataBind();
                }
            }
    
            protected void TextBox_TextChanged(object sender, EventArgs e)
            {
                TextBox textBox = (TextBox)sender;
    
                Label label = (Label)textBox.Parent.FindControl("TheLabel");
    
                label.Text = "Hello, world!";
            }
        }
    }
    

    Basically, you get the Panel object containing the related controls, then find the associated label.

    Here's what the example looks like in practice:

    enter image description here

    Note that updating the Label will require a postback. To update the label without a postback, you'll have to do some JavaScript trickery.