asp.net – 如何从Control继承而不是UserControl?

我有一个继承自UserControl的ASP.net控件:

档案1:

public partial class Controls_AllocationDuplicate : System.Web.UI.UserControl

文件2:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AllocationDuplicate.ascx.cs" Inherits="Controls_AllocationDuplicate" %>

I need the control to inherit from Control, rather than UserControl

File.aspx.cs(已更改):

public partial class Controls_AllocationDuplicate : System.Web.UI.Control

File.aspx(未更改):

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AllocationDuplicate.ascx.cs" Inherits="Controls_AllocationDuplicate" %>

当我这样做时,Visual Studio给出一个编译错误:

Make sure that the class defined in this code file matches the ‘inherits’ attribute, and that it extends the correct base class (e.g. Page or UserControl).

如何让我的控件继承自Control而不是UserControl?

系列

这个问题是正在进行的Stackoverflow系列中的一个,“模板化用户控件”:

> How to add a Templating to a UserControl?
> How to inherit from Control, rather than UserControl?
> UserControl has IsPostBack, but Control does not
> UserControl does not have public property named ContentTemplate
> How do i specify CodeFileBaseClass from web.config?

最佳答案 尝试从WebControl中继承,然后使用Register而不是Control.

与此类似:
http://www.codeproject.com/Articles/2748/Building-an-ASP-NET-custom-web-control-a-textbox-a

点赞