简体   繁体   English

用户控件继承自自定义控件

[英]user control inherits from custom control

I have an custom control that extend System.Web.UI.UserControl. 我有一个扩展System.Web.UI.UserControl的自定义控件。 Is it possible to inherit user control from this custom control? 是否可以从此自定义控件继承用户控件?

Yes. 是。 Simply swap out your custom UserControl class in your user control's code-behind. 只需在用户控件的隐藏代码中换出自定义UserControl类。 All that is necessary is that the base user control derive from System.Web.UI.UserControl as you describe. 您所需要的就是基本用户控件派生自System.Web.UI.UserControl

public partial class MyUserControl : MyBaseUserControl
{
    // ...
}

If you are not using a code-behind, you can use the Inherits attribute in the <%@ Control %> directive to specify the base class. 如果不使用隐藏代码,则可以使用<%@ Control %>指令中的Inherits属性指定基类。

So long as you have not marked the control as sealed , you can inhrit from it and create other user controls that use the functionality already written. 只要您尚未将控件标记为“已sealed ,就可以从中使用并创建使用已编写功能的其他用户控件。

So long as UserControl is part of the inheritance chain, everything should work as you would expect. 只要UserControl是继承链的一部分,一切都应该按您期望的那样工作。

Yes, in fact is very useful as you can have common methods available for all of your controls through this inherited custom control. 是的,实际上非常有用,因为您可以通过此继承的自定义控件为所有控件提供通用方法。 It is that standard practice in our shop that no control inherits directly from System.Web.UI.UserControl but from the extended control in our common library. 在我们商店中的标准做法是,没有控件直接从System.Web.UI.UserControl继承,而没有继承自公共库中的扩展控件。

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

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