3

i have a UserControl in ASP.net:

GrobSelector.ascx.cs:

...
public partial class GrobSelector : System.Web.UI.UserControl
{
   ...

i want to wrap this class in a namespace:

GrobSelector.ascx.cs:

...
namespace GrobSelectorNamespace
{
   public partial class GrobSelector : System.Web.UI.UserControl
   {
      ...
   ...       
}

Except now code fails to compile:

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

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

What's the correct way to put an ASP.net UserControl in a namespace?


Bonus Chatter

The code in-front file contains:

GrobSelector.ascx:

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

Bonus Reading

Community
  • 1
  • 1
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219

1 Answers1

7

The error you received explains it correctly.

You should change your 'in-front' file such that the Inherits attribute takes into account the namespace in which the control class resides:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="VacationSelector.ascx.cs" 
    Inherits="VacationSelectorNamespace.VacationSelector"  %>
Jeremy
  • 8,902
  • 2
  • 36
  • 44
  • i assumed the `VacationSelector.ascx/VacationSelector.ascx.cs` took care of that! – Ian Boyd May 28 '12 at 19:55
  • It can be tricky. But keep in mind that you don't necessarily have to have the same class name as the actual file. This is to say that you can have a VacationSelector.ascx.cs whose class is actually 'foo' which makes it all the more necessary to include the fully qualified name of the class. – Jeremy May 28 '12 at 19:57