Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Subclassing System.Web.UI.UserControl

Hello, I have an abstract control (CustomUserControl) that inherits System.Web.UI.UserControl so when I am creating a new user control I inherit CustomUserControl instead of UserControl.  The problem is that I can't view any controls derived from CustomUserControl in design mode.  I just get an "Object reference not set to an instance of an object" error.  These ontrols work fine when the page is rendered though.  Has anyone else had this problem?

l.r. fisher
Friday, July 11, 2003

I don't think its a problem, I guess its by design. In design mode of the IDE the object references are not instantiated and the control is not active.

Sunday, July 13, 2003

Is your CustomUserControl defined in a separate assembly outside of the web application project?  If not it will not render in the IDE. 

To fix this just create a class library file, move the CustomUserControl.cs to this class library, then add a reference to it in the web app, add it to your toolbox and you should be good to go....

Sunday, July 13, 2003

You can't add user controls to the toolbox in VS.NET. User controls don't have much in the way of design time support, unfortunately.

Chris Tavares
Tuesday, July 15, 2003

User controls are actually pretty poor feature-wise. Why not just make a custom control and be done with it? :)

Brad Wilson (
Tuesday, July 15, 2003

To Everyone - thanks for the comments and help.

Brad - I agree that custom controls are better in many ways than user controls and I do use them for certain things.  The reason I like to use user controls for a lot of things is because they are quick and easy to create.  I would like to add some functionality to all my user controls though and I think that creating a base class for them is a nice way to go.

Giorgia - Thanks for the tip.  I have now tried separating the base class to a new project and even a new solution.  The problem I have found is that in my user control base class I have contained a custom control as a protected member variable that will not render in the designer.  This apparently causes the whole user control to not render.  I am now working to find why this custom control won't render in the designer, but will render at run time.  Any ideas anyone?

l.r. fisher
Wednesday, July 16, 2003

I found out why my custom control was not rendering at design time!  In the constructor of the custom control I was getting a value from web.config using  the Configuration.Appsettings() method.  Apparently the designer did not like this because when I removed it everything was good.

l.r. fisher
Thursday, July 17, 2003

*  Recent Topics

*  Fog Creek Home