简体   繁体   English

如果不调用base.createchildcontrols()会发生什么

[英]what will happen if we don't call base.createchildcontrols()

I just would like to know what will happen if we don't put base.createchildcontrols() in the code. 我只是想知道如果不将base.createchildcontrols()放入代码中会发生什么。 Will composite control be created without calling 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();
 }
  }

The short answer is... Nothing! 简短的答案是……什么都没有! You don't need to call the base implementation (although you can always try removing it to see what happens ;-) 您无需调用基本实现(尽管您始终可以尝试将其删除以查看会发生什么;-)

Using ILSpy , we can see that CompositeControl inherits from WebControl which inherits from Control. 使用ILSpy ,我们可以看到CompositeControl继承自WebControl,而WebControl继承自Control。

CreateChildControl() is defined on Control as: CreateChildControl()在控件上定义为:

protected internal virtual void CreateChildControls()
{
}

ie It is only there to be overriden. 即它只是在那里被覆盖。

Compare this with some other controls that inherit from Control, like BaseDataList and you can see that that method has a lot of functionality for checking and rendering the output. 将此与从Control继承的其他一些控件进行比较,例如BaseDataList,您可以看到该方法具有很多检查和呈现输出的功能。

This makes sense. 这是有道理的。 Reading the MSDN documentation, here , we can see that it is for you to implement the rendering of any child controls. 这里阅读MSDN文档,我们可以看到它是实现任何子控件的呈现的对象。 Only if the class you are inhereting from requires this method to be called, then you'll have to call it. 仅当您所在的类要求调用此方法时,才必须调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM