ASP.NET 2.0: Referencing a control from another control

February 3rd, 2005
Posted by gerrod in: geek

For my current project at work, I’ve been using ASP.NET 2.0, in its pre-release state. Though I’m impressed by the improvements that Microsoft have made to the framework (in general), there’s been a few things that I’ve found really annoying. I’m hoping that they’re just bugs which will be fixed by the next beta release.

One of these such bugs involves referencing a UserControl from another UserControl. So, very roughly, lets say you have your two controls defined as follows:

public partial class FirstControl_ascx : UserControl {
   // control implementation
}

public partial class SecondControl_ascx : UserControl {
    public FirstControl_ascx firstControl;

    // control implementation
}

Previously, this was no dramas at all (of course, presuming that you have them in the same namespace, or you have the correct using statements). However, typically now I get a compile error in SecondControl that basically tells me that it doesn’t know anything about FirstControl. Worse still, it’s temperamental – sometimes it works, sometimes it doesn’t. Pah!

My only plausable work-around to date is to create an interface that encapsulates the required methods from FirstControl, and then only reference the interface from SecondControl. So far so good, but clearly this is a bug in the framework! I hope Microsoft get around to fixing it before the next release.

No comments. Be the first!