[英]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.