web自定义控件继承复合控件问题控件问题
- C# code
namespace WDemo{ [DefaultProperty("Text")] [ToolboxData("<{0}:TextButton runat=server></{0}:TextButton>")] public class TextButton : WebControl, INamingContainer { [Bindable(true)] [Category("Appearance")] [DefaultValue("")] [Localizable(true)] TextBox tb; Button b; public delegate void dl(object sender,EventArgs e); public event dl TextButtonClick; public TextButton() { tb = new TextBox(); b = new Button(); b.Text = "提交"; b.Click += new EventHandler(b_Click); } void b_Click(object sender, EventArgs e) { TextButtonClick(sender,e); } public string TextContent { get { return tb.Text; } set { tb.Text = value; } } protected override void CreateChildControls() { Controls.Add(tb); Controls.Add(b); // base.CreateChildControls(); } }}以前学校的code,加不加// base.CreateChildControls();都能正常显示两个子控件,请问加与不加的区别。调用基类的CreateChildControls()方法不知为何。
[解决办法]
如果TextButton 从 MyButton继承,而MyButton继承自WebControl
MyButton 中Add一个控件
TextButton再加入两个控件
如果你在TextButton中不调用 base.CreateChildControls();那么MyButton中的那个控件就不会被加入。
[解决办法]