C# UpdatePanel添加到";时消失的触发器;“自定义控件”;

C# UpdatePanel添加到";时消失的触发器;“自定义控件”;,c#,asp.net,updatepanel,linkbutton,C#,Asp.net,Updatepanel,Linkbutton,简而言之,问题在于,只要我将UpdatePanel的动态生成触发器作为自定义控件的子控件添加,就无法再(通过ASP.NET)找到它们 由于我正在处理的代码量相当大,所以我在较小的范围内重新创建了这个问题,这将使调试更容易 我面临的错误是: 在UpdatePanel“UpdatePanel”中找不到触发器ID为“theTrigger”的控件。 我不确定这个“自定义控件”的实现是否是正确的,但我没有编写原始的实现:我使用的是以前的开发人员编写的代码,我无法对其进行大的修改。这对我来说有点不寻常,但是

简而言之,问题在于,只要我将UpdatePanel的动态生成触发器作为自定义控件的子控件添加,就无法再(通过ASP.NET)找到它们

由于我正在处理的代码量相当大,所以我在较小的范围内重新创建了这个问题,这将使调试更容易

我面临的错误是:

在UpdatePanel“UpdatePanel”中找不到触发器ID为“theTrigger”的控件。

我不确定这个“自定义控件”的实现是否是正确的,但我没有编写原始的实现:我使用的是以前的开发人员编写的代码,我无法对其进行大的修改。这对我来说有点不寻常,但是,唉,这就是我得到的

Default.aspx MyControl.cs 任何想法都将不胜感激

编辑 我已尝试为此切换
OnPreRender
事件(可在教程中找到):


。。。希望它能修复它,但它不能。

这是我从代码后面将ascx web控件加载到aspx控件中所采用的方法

在控件中:

namespace dk_admin_site.Calculations
{
    public partial class AssignedFieldCalculation : System.Web.UI.UserControl
    {
        public static AssignedFieldCalculation LoadControl(Calculation initialData)
        {
            var myControl = (AssignedFieldCalculation) ((Page) HttpContext.Current.Handler).LoadControl(@"~\\Calculations\AssignedFieldCalculation.ascx");
            myControl._initialData = initialData;
            return myControl;
        }

        private Calculation _initialData;
        public Calculation Data { get { return _initialData; } }
        protected void Page_Load(object sender, EventArgs e) {}
    }
}
在web表单代码隐藏中:

protected void Page_Load(object sender, EventArgs e)
{

    if (this.IsPostBack)
    {
        if (ScriptManager1.AsyncPostBackSourceElementID.StartsWith("ctl00$MainContent$calc") && ScriptManager1.AsyncPostBackSourceElementID.EndsWith("$btnRemoveCalculationFromField"))
        {
            //do something on the postback
        }
        else if (ScriptManager1.AsyncPostBackSourceElementID.StartsWith("ctl00$MainContent$calc") && (ScriptManager1.AsyncPostBackSourceElementID.EndsWith("$btnMoveCalculationUp") || ScriptManager1.AsyncPostBackSourceElementID.EndsWith("$btnMoveCalculationDown")))
        {
            //do something on the postback
        }
    }


    foreach (Calculation calc in calculationCollection)
    {
        AssignedFieldCalculation asCalc = AssignedFieldCalculation.LoadControl(calc);
        asCalc.ID = "calc" + calc.UniqueXKey;
        pnlFieldCalculations.Controls.Add(asCalc);

        foreach (Control ct in asCalc.Controls)
        {
            if (ct.ID == "btnMoveCalculationDown" || ct.ID == "btnMoveCalculationUp" || ct.ID == "btnRemoveCalculationFromField")
            {
                ScriptManager1.RegisterAsyncPostBackControl(ct);
            }
        }
    }
}
需要注意的几点:
将每个控件ID添加到asp:面板(称为pnlFieldCalculations)时,需要使其唯一。 LoadControl方法允许您传递初始参数

using System;
using System.Web.UI.WebControls;

namespace TestWeb
{
   public class UselessTableWrapper : WebControl
   {
      private Table table = new Table();

      protected override void OnPreRender(EventArgs e)
      {
         Controls.Add(table);
      }

      public void AddRow(TableRow row)
      {
         table.Controls.Add(row);
      }
   }
}
protected override void RenderContents(HtmlTextWriter writer)
{
   writer.BeginRender();
   table.RenderControl(writer);
   writer.EndRender();
   base.RenderContents(writer);
}
namespace dk_admin_site.Calculations
{
    public partial class AssignedFieldCalculation : System.Web.UI.UserControl
    {
        public static AssignedFieldCalculation LoadControl(Calculation initialData)
        {
            var myControl = (AssignedFieldCalculation) ((Page) HttpContext.Current.Handler).LoadControl(@"~\\Calculations\AssignedFieldCalculation.ascx");
            myControl._initialData = initialData;
            return myControl;
        }

        private Calculation _initialData;
        public Calculation Data { get { return _initialData; } }
        protected void Page_Load(object sender, EventArgs e) {}
    }
}
protected void Page_Load(object sender, EventArgs e)
{

    if (this.IsPostBack)
    {
        if (ScriptManager1.AsyncPostBackSourceElementID.StartsWith("ctl00$MainContent$calc") && ScriptManager1.AsyncPostBackSourceElementID.EndsWith("$btnRemoveCalculationFromField"))
        {
            //do something on the postback
        }
        else if (ScriptManager1.AsyncPostBackSourceElementID.StartsWith("ctl00$MainContent$calc") && (ScriptManager1.AsyncPostBackSourceElementID.EndsWith("$btnMoveCalculationUp") || ScriptManager1.AsyncPostBackSourceElementID.EndsWith("$btnMoveCalculationDown")))
        {
            //do something on the postback
        }
    }


    foreach (Calculation calc in calculationCollection)
    {
        AssignedFieldCalculation asCalc = AssignedFieldCalculation.LoadControl(calc);
        asCalc.ID = "calc" + calc.UniqueXKey;
        pnlFieldCalculations.Controls.Add(asCalc);

        foreach (Control ct in asCalc.Controls)
        {
            if (ct.ID == "btnMoveCalculationDown" || ct.ID == "btnMoveCalculationUp" || ct.ID == "btnRemoveCalculationFromField")
            {
                ScriptManager1.RegisterAsyncPostBackControl(ct);
            }
        }
    }
}