【问题标题】:Using ASPX Web User Control with Visual Studio 2010在 Visual Studio 2010 中使用 ASPX Web 用户控件
【发布时间】:2012-02-18 04:47:34
【问题描述】:

我正在尝试在我的一个 APSX 页面中实施 Web 用户控件,但不断收到以下警告:

元素“IntFilter”不是已知元素。如果网站中存在编译错误,或者缺少 web.config 文件,就会发生这种情况。

用户控件与 aspx 页面在同一个 web 项目中定义。

问题:
如何解决此警告(我不想将控件移动到单独的项目)?
另外,我需要做什么才能为该控件启用 IntelliSense,以便我可以从 ASPX 设置其 FilterTypeSelection 属性?

“~/FilterControls/IntFilter.ascx”的代码

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="IntFilter.ascx.vb" Inherits="StaffSupport.Filters.IntegerFilter" %>
<asp:DropDownList ID="typeFilterDropDownList" runat="server">
    <asp:ListItem Selected="True"  Text ="Any"          Value="-1" />
    <asp:ListItem Selected="False" Text ="Equal"        Value= "0" />
</asp:DropDownList><br />
<asp:TextBox ID="TextBox1" runat="server" /><asp:CheckBox ID="CheckBox1" runat="server" Text="Inclusive" /><br />
<asp:TextBox ID="TextBox2" runat="server" /><asp:CheckBox ID="CheckBox2" runat="server" Text="Inclusive" /><br />

“~/FilterControls/IntFilter.ascx.vb”的代码

Namespace Filters
    Public Class IntegerFilter
        Inherits System.Web.UI.UserControl

        Public Enum NumberFilterTypes As SByte
            Any = -1
            Equals = 0
        End Enum

        Public Property FilterTypeSelection As NumberFilterTypes
            Get
                Dim value As SByte
                If Not  Integer.TryParse(typeFilterDropDownList.SelectedValue, value) Then
                    value = -1
                End If

                Return CType(value, NumberFilterTypes)
            End Get
            Set(value As NumberFilterTypes)
                typeFilterDropDownList.SelectedValue = CSByte(value)
            End Set
        End Property

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        End Sub

    End Class
End Namespace

“OpenCases.aspx”的代码

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/StaffSite.Master" CodeBehind="OpenCases.aspx.vb" Inherits="StaffSupport.OpenCases" %>
<%@ Register TagPrefix="filters" TagName="IntFilter" src="~/FilterControls/IntFilter.ascx" %>
<asp:Content ID="bodyContent" ContentPlaceHolderID="cphBody" runat="server">
    ID<br />
    <filters:IntFilter ID="IntFilter1" runat="server" />
</asp:Content>

“OpenCases.aspx.vb”的代码

    Public Class OpenCases
        Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Page.ViewStateMode = UI.ViewStateMode.Disabled
    End Sub

2012 年 2 月 21 日更新:
修复了“过滤器”与“过滤器”未匹配的问题。

另外值得注意的是,如果您将控件从解决方案资源管理器拖到设计视图中的页面,它将添加您需要的引用(尽管它仍在为我生成警告)。如果你将它拖到源视图中的页面,它会在元素中添加一个带有 href 的标签。

2012 年 2 月 21 日更新 b:
找到了解决办法,请看下面我的回答。

【问题讨论】:

  • 我认为您需要将此控件的引用添加到您的项目中

标签: asp.net vb.net visual-studio-2010 webusercontrol


【解决方案1】:

显然您必须同时引用 ASCX 页面程序集。
如果您将 ASCX 页面从“解决方案资源管理器”窗口拖到您正在编辑的页面的设计视图中,它将添加 ASCX 页面的引用,但您必须手动添加程序集引用。

OpenCases.aspx

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/StaffSite.Master" CodeBehind="OpenCases.aspx.vb" Inherits="StaffSupport.OpenCases" %>
<%@ Register Assembly="StaffSupport" Namespace="StaffSupport.Filters" TagPrefix="filters" %><%-- Assembly Reference --%>
<%@ Register TagPrefix="filters" TagName="IntFilter" src="~/FilterControls/IntFilter.ascx" %>
<asp:Content ID="bodyContent" ContentPlaceHolderID="cphBody" runat="server">
    ID<br />
    <filters:IntFilter ID="IntFilter1" runat="server" />
</asp:Content>

注意:注意对象类型冲突。例如以下也可以工作:

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/StaffSite.Master" CodeBehind="OpenCases.aspx.vb" Inherits="StaffSupport.OpenCases" %>
<%@ Register Assembly="StaffSupport" Namespace="StaffSupport.Filters" TagPrefix="pre1" %><%-- Assembly Reference --%>
<%@ Register TagPrefix="pre2" TagName="IntFilter" src="~/FilterControls/IntFilter.ascx" %>
<asp:Content ID="bodyContent" ContentPlaceHolderID="cphBody" runat="server">
    ID<br />
    <pre1:IntFilter ID="IntFilter1" runat="server" />
</asp:Content>

这就是为什么它在我发布后星期五开始为我工作的原因,我添加了一个实现 System.Web.UI.WebControls.TextBox 的自定义控件,因此我可以从工具箱中拖放它。由于它在同一个命名空间中,因此控件在将控件添加到页面时添加了程序集引用。

注意:如果您正在引用项目中包含的 dll 文件,那么您可能需要删除页面注册,构建,然后重新添加页面注册。否则编译器可能会抱怨dll文件不在bin中。

更新: 2013/04/18
如果UserControl 未在同一命名空间中定义,您似乎只需要添加程序集引用。

  • 如果在 Proj.Presentation 中定义了父对象,并且在 Proj.Presentation 中定义了 UserControl,那么您应该不需要程序集引用。
  • 如果在 Proj.Page 中定义了父对象,并且在 Proj.Page.UserControl 中定义了 UserControl,那么您应该不需要程序集引用。
  • 如果在 Proj.Page 中定义了parrent,并且在 Proj.UserControl 中定义了UserControl,那么您需要程序集引用。

【讨论】:

    【解决方案2】:

    控件声明为:

    <%@ Register TagPrefix="filters"
    

    在标记中

    <filter:IntFilter
    

    这些必须匹配。

    【讨论】:

      【解决方案3】:

      您注册的前缀与您尝试使用的前缀不同。

      你可以改变这个:

      <filter:IntFilter ID="IntFilter1" runat="server" />
      

      到这里:

      <filters:IntFilter ID="IntFilter1" runat="server" />
      

      或者改变这个:

      <%@ Register TagPrefix="filters" TagName="IntFilter" 
      

      到这里:

      <%@ Register TagPrefix="filter" TagName="IntFilter" 
      

      【讨论】:

        【解决方案4】:
        Close Visual Studio, delete the schema cache, and re-open Visual Studio. You can find the schemas under something like:
        
        C:\Users\Pavel\AppData\Roaming\Microsoft\VisualStudio\10.0\ReflectedSchemas
        
        It is safe to delete all files in this folder.
        

        删除上面那个文件夹的内容,一切正常。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-04-14
          • 2011-03-27
          • 2011-02-20
          • 2011-04-04
          • 2023-03-10
          • 2011-11-06
          • 1970-01-01
          相关资源
          最近更新 更多