【问题标题】:what will happen if we don't call base.createchildcontrols()如果我们不调用 base.createchildcontrols() 会发生什么
【发布时间】:2012-01-13 15:41:50
【问题描述】:

我只是想知道如果我们不将 base.createchildcontrols() 放入代码中会发生什么。是否会在不调用 base.createchildcontrols() 的情况下创建复合控件?

 [ToolboxData("<{0}:Login runat=server></{0}:Login>")]
public class Login : CompositeControl
{
 private TextBox txtUsername = new TextBox();
private TextBox txtPassword = new TextBox();
private Button btnLogin = new Button();

protected override void CreateChildControls()
{
txtUsername.ID = "txtUsername";
txtPassword.ID = "txtPassword";
txtPassword.TextMode = TextBoxMode.Password;
btnLogin.ID = "btnLogin";
btnLogin.Text = "Login";

Controls.Add(txtUsername);
Controls.Add(txtPassword);
Controls.Add(btnLogin);

base.CreateChildControls();
 }
  }

【问题讨论】:

    标签: asp.net createchildcontrols


    【解决方案1】:

    简短的回答是……什么都没有!您不需要调用基本实现(尽管您可以随时尝试删除它以查看会发生什么;-)

    使用ILSpy,我们可以看到CompositeControl继承自WebControl,而WebControl继承自Control。

    CreateChildControl() 在 Control 上定义为:

    protected internal virtual void CreateChildControls()
    {
    }
    

    即它只能被覆盖。

    将其与其他一些从 Control 继承的控件(如 BaseDataList)进行比较,您会发现该方法具有很多检查和呈现输出的功能。

    这是有道理的。阅读 MSDN 文档,here,我们可以看到它是为您实现任何子控件的呈现。只有当您继承的类需要调用此方法时,您才必须调用它。

    【讨论】:

    • 嗨,我明白了。如果不调用 base.createchildcontrols() 我们将失去它为我们的复合控件提供的功能......对吗?
    • @dotnetrocks 在这种情况下,不,至于 CompositeControl 它什么都不做。但这可能不适用于其他控件。
    猜你喜欢
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    相关资源
    最近更新 更多