【问题标题】:Handling AsyncFileUpload's UploadComplete event inside repeater在转发器中处理 AsyncFileUpload 的 UploadComplete 事件
【发布时间】:2020-08-13 22:00:59
【问题描述】:

我有一个中继器,其中包含一个 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>

这看起来正确吗?在我弄清楚可以访问中继器内部的控件之前,我无法对其进行测试,但我想我会与您核实是否有意义。

【问题讨论】:

    标签: c# asp.net repeater asyncfileupload


    【解决方案1】:

    我不熟悉 AsynFileUpload 工具,但我可以向您展示如何在一般情况下访问与 sender 控件相同的面板中的标签。

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

    <%@ 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 对象,然后找到相关的标签。

    这是示例在实践中的样子:

    请注意,更新标签将需要回发。要在不回发的情况下更新标签,您必须使用一些 JavaScript 技巧。

    【讨论】:

      猜你喜欢
      • 2019-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-27
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多