在最近的一次采访中,我认为我对这个问题有合理的答案,但我对此进行了轰炸.:(
我一直在网上浏览并找到一些关于这个主题的文章,但我仍然无法弄清楚它们之间的区别.我有下面的代码显示,如果我从CompositeControl继承它完美的工作但不是如果我从WebControl继承.(它们都呈现代码,但只有CompositeControl处理事件)
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestLibrary
{
public class TemplateControl : CompositeControl
{
TextBox txtName = new TextBox();
TextBox txtEmail = new TextBox();
Button btnSend = new Button();
private void SetValues()
{
btnSend.Text = "Skicka";
}
protected override void CreateChildControls()
{
SetValues();
this.Controls.Add(new LiteralControl("Namn: "));
this.Controls.Add(txtName);
this.Controls.Add(new LiteralControl("<br />"));
this.Controls.Add(new LiteralControl("Email: "));
this.Controls.Add(txtEmail);
this.Controls.Add(new LiteralControl("<br />"));
btnSend.Command += new CommandEventHandler(btnSend_Command);
this.Controls.Add(btnSend);
}
void btnSend_Command(object sender, CommandEventArgs e)
{
this.Page.Response.Write("Du har nu klickat på skicka-knappen! <br /><br />"); …Run Code Online (Sandbox Code Playgroud)