[英]Derive user control from custom user control base class
我想创建一个用户控件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" %>
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)
{
}
}
错误
The type or namespace name 'BaseUserControl' could not be found
使用ASP.NET Web站点(与Web项目相关)时,需要将<@Reference>
元素添加到DerivedUserControl.ascx。
从MSDN它...
指示应该动态编译位于某个虚拟路径的另一个用户控件,页面源文件或任意文件,并将其链接到声明此指令的当前ASP.NET文件(网页,用户控件或母版页)。
<%@ Reference VirtualPath="~/FolderName1/BaseUserControl.ascx" %>
完成后,您可以像这样引用它
public partial class DerivedUserControl : ASP.foldername1.baseusercontrol_ascx
FolderName1是BaseUserControl所在的文件夹。
为您的基类创建一个名为BaseUserControl.cs
的常规类/ .cs:
public class BaseUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
问题似乎是DerivedUserControl.ascx无法访问包含BaseUserControl的DLL。 确保添加对dll的引用并复制local = true。
这不编译:
namespace MyBase
{
public class BaseUserControl : System.Web.UI.UserControl
{ }
}
public class DerivedUserControl : BaseUserControl
{ }
这确实编译:
namespace MyBase
{
public class BaseUserControl : System.Web.UI.UserControl
{ }
}
public class DerivedUserControl :MyBase.BaseUserControl
{ }
所以几乎添加名称空间+点的名称+基类的名称。 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.