I have been looking around on the web and found some articles about the topic, but i still can't figure out the difference between them. I have the code show below, if i inherit from a CompositeControl it works perfectly but not if i inherit from a WebControl. (They both render the code, but only the CompositeControl handles the event)
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 />");
}
}
}
So when i click the button and the control is rendered as a WebControl, nothing happens. But if i change the WebControl to a CompositeControl, the text is printed out. Why? Whats the difference between the WebControl and the CompositeControl?