Asp.NET服务器控件回发

Aar*_*ron 2 c# asp.net events user-controls postback

我有一个想要创建的控件.这是我要完成的一个简单示例.

我希望控件包含一个按钮.

Button b = new Button();
b.Text = "Test";
b.Click += new EventHandler(b_Click);

this.Controls.Add(b);
Run Code Online (Sandbox Code Playgroud)

现在,控件呈现正常,按钮显示在页面上.我遇到的问题的核心是b_Click事件处理程序永远不会被触发.

protected void b_Click(object sender, EventArgs e)
{
    throw new NotImplementedException();
}
Run Code Online (Sandbox Code Playgroud)

这里的任何帮助将不胜感激.我不想在这里使用用户控件纯粹是出于自私的原因,并希望将其完全封装在一个DLL中.

提前致谢.

编辑**

namespace ClassLibrary1
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
    public class WebCustomControl1 : WebControl
    {

        protected override void CreateChildControls()
        {
            Button b = new Button();
                    b.ID = "button";
            b.Text = "Click Me";
            b.Click += new EventHandler(b_Click);

            this.Controls.Add(b);

            base.CreateChildControls();
        }

        protected void b_Click(object sender, EventArgs e)
        {
            this.Controls.Add(new LiteralControl("<p>Click!</p>"));
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

所以从评论中我试过这个.最简单的例子,仍然没有去.有什么东西我从根本上失踪了吗?

Aar*_*ron 6

public class WebCustomControl1 : WebControl
Run Code Online (Sandbox Code Playgroud)

需要

public class WebCustomControl1 : WebControl, INamingContainer
Run Code Online (Sandbox Code Playgroud)

而已.这就是使这个回发问题发挥作用所需要的一切.