在转发器中处理AsyncFileUpload的UploadComplet事件

时间:2018-04-27 15:24:05

标签: c# asp.net repeater asyncfileupload

我有一个转发器,在其他控件中有一个AsyncFileUpload和一个错误标签都嵌入在面板内(常规,而不是更新面板)。在AFU的UploadComplete事件中,我需要访问面板和标签;我可以使用“sender”参数访问AFU本身:

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

如何确保访问转发器内的正确面板和标签?

此外,当点击位于转发器外部的“提交”按钮时,我正在使用以下内容确保所有文件立即上传并调用js函数“sendResponse()”进行回发以处理所有转发器项目。

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

这看起来是否正确?直到我弄清楚在转发器中访问控件之后我才测试它,但是如果它有意义的话我想和你一起检查。

1 个答案:

答案 0 :(得分:0)

我不熟悉AsynFileUpload工具,但我可以向您展示如何在与sender控件相同的面板中访问Label。

我设置了一个结构大致相同的示例页面:

<%@ 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>

以下是代码隐藏:

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!";
        }
    }
}

基本上,您获得包含相关控件的Panel对象,然后找到关联的标签。

以下是实践中的示例:

enter image description here

请注意,更新Label需要回发。要在没有回发的情况下更新标签,您将不得不做一些JavaScript技巧。