Asp.net

自定義控制項變為通用“UserControl”,而不是其在 Designer 類中的實際類型

  • December 24, 2015

我在 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”不是“System.Web.UI.UserControl”的成員。

在designer.vb頁面中我看到:

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

我如何確保它變成:

Protected WithEvents mycontrol1 As Global.Mynamespace.Controls.MyControl

?

您的 ascx 文件無法訪問,因為它位於庫中

您需要將 ascx 文件保存為庫的嵌入式資源,並將其作為外部資源載入到 Web 應用程序中。

您可以查閱此連結以獲取更多資訊。

如果你想共享你的控制項,我建議創建 UserControl 而不是 CustomControl。不幸的是,還有更多的工作,因為設計師不可用

確保MyControlGlobal.Mynamespace.Controls.MyControl. 它繼承了這個命名空間,但似乎這應該是定義它的命名空間。當然,還要確保定義了 MyCustomProperty。

引用自:https://stackoverflow.com/questions/34277764