从派生自定义用户控件的基类的用户控件 [英] Derive user control from custom user control base class

查看:150
本文介绍了从派生自定义用户控件的基类的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个用户控件 DerivedUserControl.ascx 的派生形成另一个用户控件 BaseUserControl.ascx 。该基地用户控件需要从 System.Web.UI.UserControl 派生的。这些用户控件在不同的文件夹中定义。因为我使用的Visual Studio 2010网站项目(我不能切换到Web应用程序项目),这些用户控件没有一个命名空间中定义的。

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

我什么都试过了我能想象,没有成功。任何帮助将大大AP preciated。


BaseUserControl.ascx

 <%@控制语言=C#AutoEventWireup =真codeFILE =BaseUserControl.ascx.cs继承=BaseUserControl%GT;

BaseUserControl.ascx.cs

 公共部分类BaseUserControl:System.Web.UI.UserControl
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {    }
}

DerivedUserControl.ascx

 <%@控制语言=C#AutoEventWireup =真codeFILE =DerivedUserControl.ascx.cs继承=DerivedUserControl%GT;

DerivedUserControl.ascx.cs

 公共部分类DerivedUserControl:BaseUserControl
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {    }
}

错误

 类型或命名空间名称'BaseUserControl'找不到


解决方案

在使用ASP.NET网站(如结合在一个Web项目),你需要添加的 < @参考> 元素的DerivedUserControl.ascx。

从MSDN吧...


  

表示另一个用户控制网页的源文件,或任意
  位于一些虚拟路径文件应该是动态编译
  针对目前ASP.NET文件(网页,用户控件或链接
  母版页),其中该指令声明。


<%@参考VirtualPath =〜/ FolderName1 / BaseUserControl.ascx%GT;

一旦你这样做,你可以参考它像这样

 公共部分类DerivedUserControl:ASP.foldername1.baseusercontrol_ascx

在哪里FolderName1是你BaseUserControl是在文件夹中。

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.

这篇关于从派生自定义用户控件的基类的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆