How to specify a namespace for an ASP.net UserCont

2019-05-11 16:41发布

问题:

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

  • Putting a Web User Control (ascx) in a namespace?
  • namespace in asp.net usercontrol
  • Getting namespace name not found for ASP.net user control

回答1:

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