自定义控件变为通用 "UserControl" 而不是其在设计器中的实际类型 class

Custom control becomes generic "UserControl" and not its actual type in Designer class

我在 ASP.NET 中有一个自定义控件(代码后面的 VB.NET),用 ASCX 定义:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="MyControl.ascx.vb" Inherits="Mynamespace.Controls.MyControl" %>

<!-- some html and other custom controls-->

在后面的代码中:

Namespace Controls

    Public Class MyControl
        Inherits System.Web.UI.UserControl

这是在图书馆设置的。另一个项目在页面中使用该控件:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="mypage.aspx.vb" 
    Inherits="myproject.mypage" culture="auto" meta:resourcekey="Page" uiculture="auto" 
    Transaction="RequiresNew" MasterPageFile="Mynamespace.Master" 
    Theme="ThemeBase2" StylesheetTheme="ThemeBase2" %>

<%@ Register tagprefix="Controls" tagname="MyControl" src="../Controls/MyControl.ascx" %>

<%-- some asp.net --%>

<Controls:MyControl ID="mycontrol1" runat="server" 
                    MyCustomProperty="value" />

但是,当我构建时,我收到一条错误消息

'MyCustomProperty' is not a member of 'System.Web.UI.UserControl'.

在 designer.vb 页面中我看到:

Protected WithEvents mycontrol1 As Global.System.Web.UI.UserControl

我如何确保它变成:

Protected WithEvents mycontrol1 As Global.Mynamespace.Controls.MyControl

?

确保 MyControl 定义在 Global.Mynamespace.Controls.MyControl 内。它继承了这个命名空间,但似乎这应该是定义它的命名空间。当然,还要确保定义了 MyCustomProperty。

您的 ascx 文件无法访问,因为它在库中

您需要将 ascx 文件保存为库的嵌入式资源,并将其作为外部资源加载到您的 Web 应用程序中。

您可以参考此 link 了解更多信息。

如果您想共享您的控件,我建议创建 UserControl 而不是 CustomControl。不幸的是,由于设计器无法使用,因此工作量更大