简体   繁体   English

在其他自定义控件中加载WebControl:从不触发Load方法

[英]Loading WebControl inside other custom control: Load method is never fired

I have a simple class inheriting from WebControl as follow: 我有一个继承自WebControl的简单类,如下所示:

public class Instrument : WebControl
{
    private SpecialItem myItem = new SpecialItem("ABC");

    protected override void Render(HtmlTextWriter output)
    {
       output.Write("<div>");
       myItem.RenderControl(output);
       output.Write("</div>");
    }
}

Once the control is added to a web page, the SpecialItem gets displayed but its inner "Load" method is never fired which makes it unusable. 将控件添加到网页后,将显示SpecialItem ,但其内部“Load”方法永远不会被触发,这使得它无法使用。 If I use the SpecialItem control on its own then it works as expected. 如果我自己使用SpecialItem控件,那么它按预期工作。 How do you I make the load method from the SpecialItem control get executed? 你如何从SpecialItem控件中执行load方法?

Add myItem to the Controls collection of your custom control. myItem添加到Controls集合中。 You typically do this (as well as instantiating the child controls) by overriding the method CreateChildControls : 您通常通过重写CreateChildControls方法来执行此操作(以及实例CreateChildControls控件):

protected override void CreateChildControls()
{
    myItem = new SpecialItem("ABC");
    Controls.Add(myItem);
}

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

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