How to inherit from Control, rather than UserContr

2019-01-15 20:16发布

I have an ASP.net control that inherits from UserControl:

File 1:

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

File 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 (changed):

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

File.aspx (not changed):

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

When i do this, Visual Studio gives a compile error:

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).

How can I have my control inherit from Control rather than UserControl?

Series

This question is one in the ongoing Stackoverflow series, "Templating user controls":

2条回答
爷的心禁止访问
2楼-- · 2019-01-15 20:48

You try to make a custom control and not a user control in this case you can't use ascx file to hold your design but need to design them pragmatically.

查看更多
地球回转人心会变
3楼-- · 2019-01-15 20:56

Try inherting from WebControl,then use Register instead of Control. Similar to this: http://www.codeproject.com/Articles/2748/Building-an-ASP-NET-custom-web-control-a-textbox-a

查看更多
登录 后发表回答