4

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":

aggsol
  • 2,343
  • 1
  • 32
  • 49
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • 1
    *WHY* do you need to inherit from Control instead of UserControl? That's legitimate of course, but from the link you included, I don't understand what you're trying. – aquinas Sep 07 '12 at 18:17
  • @aquinas: Because `UserControl` does not have a public property named `ContentTemplate` – Ian Boyd Sep 07 '12 at 19:52
  • Ahh, it's getting designer support. Understood. Yeah, it's never worked for me either, but then the only time I'm in the Designer is when I go "Oh crap! I accidentally hit the designer button, now I have to wait 5 minutes." – aquinas Sep 07 '12 at 20:52

2 Answers2

1

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

Brian
  • 1,845
  • 1
  • 22
  • 37
1

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.

MichaelT
  • 7,574
  • 8
  • 34
  • 47