简体   繁体   中英

Derive user control from custom user control base class

I want to create a user control DerivedUserControl.ascx that derives form another user control BaseUserControl.ascx . The base user control derives from System.Web.UI.UserControl as required. These user controls are defined in different folders. Because I'm using a Visual Studio 2010 Web Site project (I'm not able to switch to Web Application project), these user controls are not defined inside a 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 doesn't know what .ascx file defines the base class). Is there a way resolve this issue?

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

When using ASP.NET Web Site (as apposed to a Web Project) you need to add <@Reference> element to your DerivedUserControl.ascx.

From MSDN it...

Indicates that another user control, page source file, or arbitrary file located at some virtual path should be dynamically compiled and linked against the current ASP.NET file (Web page, user control, or master page) in which this directive is declared.

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

Once you've done that you can reference it like so

public partial class DerivedUserControl : ASP.foldername1.baseusercontrol_ascx

Where FolderName1 is the folder your BaseUserControl is in.

Create a regular class/.cs for your base class called BaseUserControl.cs :

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

    }
}

The problem appears to be that the DerivedUserControl.ascx does not have access to the DLL that contains the BaseUserControl. Make sure that you add a reference to the dll and have copy local = true.

This does not compile:

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

This does compile:

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

So pretty much add the name of the namespace + dot + the name of your base class. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM