Derive user control from a user base user control class

I want to create a user control DerivedUserControl.ascxthat displays the form of another user control BaseUserControl.ascx. Management of the basic user comes from System.Web.UI.UserControlas needed. These user controls are defined in different folders. Since I am using a Visual Studio 2010 Web Site project (I cannot switch to a web application project), these user controls are not defined inside the namespace.

My problem is that when I try to compile the project, the base class of the derived user control cannot be resolved (obviously, because the compiler does not know which .ascx file defines the base class). Is there any way to solve this problem?

I tried everything I could imagine without success. Any help would be greatly appreciated.

BaseUserControl.ascx

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

BaseUserControl.ascx.cs

public partial class BaseUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

DerivedUserControl.ascx

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

DerivedUserControl.ascx.cs

public partial class DerivedUserControl : BaseUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Error

The type or namespace name 'BaseUserControl' could not be found
+5
source share
4 answers

When using the ASP.NET website (as an attachment to a web project) you need to add an element <@Reference>to your DerivedUserControl.ascx,

From MSDN, this is ...

, , , , ASP.NET(-, master page), ​​ .

<%@ Reference VirtualPath="~/FolderName1/BaseUserControl.ascx" %>

,

public partial class DerivedUserControl : ASP.foldername1.baseusercontrol_ascx

FolderName1 - , BaseUserControl.

+2

/.cs BaseUserControl.cs:

public class BaseUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
+1

The problem is that DerivedUserControl.ascx does not have access to the DLL that contains BaseUserControl. Make sure you add the link to the DLL and copy local = true.

0
source

This will not compile:

namespace MyBase
{
    public class BaseUserControl : System.Web.UI.UserControl
    { }
}
public class DerivedUserControl : BaseUserControl
{ }

This compiles:

namespace MyBase
{
    public class BaseUserControl : System.Web.UI.UserControl
    { }
}
public class DerivedUserControl :MyBase.BaseUserControl
{ }

To a large extent, add the namespace name + dot + name of your base class. Good luck

0
source

All Articles