简体   繁体   English

添加到“自定义控件”时触发UpdatePanel消失

[英]Trigger for UpdatePanel disappearing when added to a “custom control”

Basically, in a nutshell, the problem is that dynamically generated triggers for an UpdatePanel can no longer be found (by ASP.NET) as soon as I add them as children of a custom control. 简而言之,问题在于,一旦我将它们添加为自定义控件的子级,就不再能够(通过ASP.NET)找到动态生成的UpdatePanel触发器。

Since the amount of code I'm working on is quite substantial I've recreated the problem on a smaller scale, which will make it easier to debug. 由于我正在处理的代码量很大,因此我以较小的规模重新创建了该问题,这将使调试更加容易。

The error thrown in my face is: 我脸上的错误是:

A control with ID 'theTrigger' could not be found for the trigger in UpdatePanel 'updatePanel'.

I'm not sure whether this implementation of a "custom control" is the right way to go about it, but I did not write the original implementation: I'm working with code written by a previous developer to which I cannot make large modifications. 我不确定这种“自定义控件”的实现是否正确,但是我没有编写原始的实现:我正在使用以前的开发人员编写的代码,无法对其进行大量修改。 It looks a little unusual to me, but, alas, this is what I've been given. 在我看来,这有点不寻常,但是,las,这就是我得到的。

Default.aspx Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWeb.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Panel runat="server" ID="panel">
      </asp:Panel>

      <asp:ScriptManager ID="scriptManager" runat="server"></asp:ScriptManager>

      <asp:UpdatePanel runat="server" ID="updatePanel" UpdateMode="Conditional">
         <ContentTemplate> 
            <asp:Label ID="lblSomething" runat="server"></asp:Label>
         </ContentTemplate>
      </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

Default.aspx.cs Default.aspx.cs

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

namespace TestWeb
{
   public partial class Default : Page
   {
      protected void Page_Load(object sender, EventArgs e)
      {
         UselessTableWrapper table = new UselessTableWrapper();
         TableRow tr = new TableRow();
         TableCell td = new TableCell();

         LinkButton button1 = new LinkButton { ID = "theTrigger", Text = "Click Me" };
         button1.Click += button1_Click;
         td.Controls.Add(button1);

         tr.Controls.Add(td);
         table.AddRow(tr);

         panel.Controls.Add(table);
         // ### uncomment these lines (and comment the one above) to see it working
         // ### without the custom control
         /*
         Table realTable = new Table();
         realTable.Controls.Add(tr);
         panel.Controls.Add(realTable);
         */

         updatePanel.Triggers.Add(new AsyncPostBackTrigger { ControlID = "theTrigger", EventName = "Click" });
         scriptManager.RegisterAsyncPostBackControl(button1);
      }

      protected void button1_Click(object sender, EventArgs e)
      {
         lblSomething.Text = "Random number: " + new Random().Next(100);
         updatePanel.Update();
      }
   }
}

MyControl.cs MyControl.cs

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

Any ideas would be greatly appreciated. 任何想法将不胜感激。

Edit 编辑

I've tried switching the OnPreRender event for this (found in a tutorial): 我尝试为此切换OnPreRender事件(在教程中找到):

protected override void RenderContents(HtmlTextWriter writer)
{
   writer.BeginRender();
   table.RenderControl(writer);
   writer.EndRender();
   base.RenderContents(writer);
}

... hoping that it would fix it, but it does not. ...希望能解决它,但不能解决。

this is the approach that I've taken with loading a ascx web control inside an aspx control from the code behind. 这是我从后面的代码在aspx控件中加载一个ascx Web控件时采用的方法。

In the control: 在控件中:

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) {}
    }
}

in the web form code behind: 在后面的网络表单代码中:

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

A few things to note: 注意事项:
You need to make each control ID unique when adding it to the asp:Panel (called pnlFieldCalculations). 将每个控件ID添加到asp:Panel(称为pnlFieldCalculations)时,需要使其唯一。 The LoadControl method allows you to pass initial arguments LoadControl方法允许您传递初始参数

暂无
暂无

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

相关问题 如何使用UpdatePanel以编程方式添加到页面的方式创建和使用自定义控件 - How to create and use custom control with UpdatePanel added to the page programmatically 如何从外部使用触发器控件更新UpdatePanel - How to update an UpdatePanel with a trigger control from outside of it 在 UpdatePanel 中找不到触发器的 ID 控件 - A control with ID could not be found for the trigger in UpdatePanel wpf中的自定义控件,添加控件时添加和删除默认值 - custom control in wpf, add and remove default values when control added UpdatePanel触发器错误。无法在UpdatePanel中的触发器的关联控件上找到事件 - UpdatePanel Trigger Error. Could not find an event on associated control for the trigger in UpdatePanel 如果在Page_Load中添加了UpdatePanel,则无法在ascx控件中使用 - UpdatePanel not working in ascx control if is added on Page_Load 在 UpdatePanel 中时菜单控件 CSS 中断 - Menu control CSS breaks when inside UpdatePanel 在UpdatePanel&#39;pnlUpdate&#39;中找不到触发器ID为&#39;btnUpload&#39;的控件 - A control with ID 'btnUpload' could not be found for the trigger in UpdatePanel 'pnlUpdate' ASP.Net 从 UpdatePanel 触发用户控件下拉列表更改 - ASP.Net Trigger user control dropdownlist change from UpdatePanel 在UpdatePanel&#39;UpdatePanel3&#39;中找不到触发器ID为&#39;imgbtn_excel_gridout&#39;的控件 - A control with ID 'imgbtn_excel_gridout' could not be found for the trigger in UpdatePanel 'UpdatePanel3'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM