从自定义用户控件基类派生用户控件

Cod*_*bie 5 c# asp.net user-controls

我想创建一个DerivedUserControl.ascx从另一个用户控件派生的用户控件BaseUserControl.ascx.基本用户控件根据System.Web.UI.UserControl需要派生.这些用户控件在不同的文件夹中定义.因为我正在使用Visual Studio 2010网站项目(我无法切换到Web应用程序项目),所以这些用户控件未在命名空间内定义.

我的问题是,当我尝试编译项目时,无法解析派生用户控件的基类(显然是因为编译器不知道.ascx文件定义了基类).有办法解决这个问题吗?

我尝试了所有我能想象到的东西,没有成功.任何帮助将不胜感激.

BaseUserControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="BaseUserControl.ascx.cs" Inherits="BaseUserControl" %>
Run Code Online (Sandbox Code Playgroud)

BaseUserControl.ascx.cs

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

    }
}
Run Code Online (Sandbox Code Playgroud)

DerivedUserControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="DerivedUserControl.ascx.cs" Inherits="DerivedUserControl"  %>
Run Code Online (Sandbox Code Playgroud)

DerivedUserControl.ascx.cs

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

    }
}
Run Code Online (Sandbox Code Playgroud)

错误

The type or namespace name 'BaseUserControl' could not be found
Run Code Online (Sandbox Code Playgroud)

Con*_*rix 2

使用 ASP.NET 网站(与 Web 项目相对)时,您需要将<@Reference>元素添加到 DerivedUserControl.ascx 中。

从 MSDN 来看...

指示应针对声明此指令的当前 ASP.NET 文件(网页、用户控件或母版页)动态编译和链接位于某个虚拟路径的另一个用户控件、页面源文件或任意文件。

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

完成后,您可以像这样引用它

public partial class DerivedUserControl : ASP.foldername1.baseusercontrol_ascx
Run Code Online (Sandbox Code Playgroud)

其中FolderName1是您的BaseUserControl所在的文件夹。