简体   繁体   English

来自用户控件ASP.NET的AjaxFileUpload回发事件

[英]AjaxFileUpload postback event from a usercontrol ASP.NET

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. 我在一个用户控件中有一个AjaxFileUpload控件,该控件在Postback上动态加载,这里的问题是,一旦文件上传IsPostBack为false,由于该用户控件未加载,因此IsPostBack为false,导致不会触发OnUploadCompleteAll事件。

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? 我发现AjaxFileUpload控件具有自己的回发属性AjaxFileUpload.IsInFileUploadPostBack ,如何从WebForm1.aspx主页面访问此属性?

When an event is triggered from AjaxFileUpload i would like to check IsInFileUploadPostBack on page load of WebForm1.aspx and then load the usercontrol. 当从AjaxFileUpload触发事件时,我想在WebForm1.aspx页面加载时检查IsInFileUploadPostBack ,然后加载usercontrol。

here is the code. 这是代码。

WebForm1.aspx 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 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>

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. Ajax控件不会创建实际的回发,这就是为什么它总是错误的原因。 For what you seem to want you should use a NON-ajax file upload. 对于您似乎想要的内容,应该使用NON-ajax文件上传。

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. 由于只需要加载控件,然后我才能访问AjaxFileUpload.IsInFileUploadPostBack属性,因此我决定采用非传统的方法。

Added below code to Page_Init 将以下代码添加到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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM