簡體   English   中英

在Visual Studio 2010中使用ASPX Web用戶控件

[英]Using ASPX Web User Control with Visual Studio 2010

我試圖在我的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/02/21:
修復了“過濾器”與“過濾器”錯過匹配的問題。

還要注意的是,如果將控件從“解決方案資源管理器”拖動到“設計”視圖中的頁面,它將添加您需要的引用(盡管它仍在為我生成警告)。 如果將其拖動到源代碼視圖中的頁面,它將在元素中添加帶有href的標簽。

更新2012/02/21 b:
找到了解決方案,請參閱下面的答案。

顯然,您必須同時引用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中定義了對象,而在Proj.UserControl中定義了UserControl ,則需要程序集引用。

該控件聲明為:

<%@ Register TagPrefix="filters"

並在標記中

<filter:IntFilter

這些必須匹配。

您正在注冊與嘗試使用的前綴不同的前綴。

您可以更改以下內容:

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

對此:

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

或更改此:

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

對此:

<%@ Register TagPrefix="filter" TagName="IntFilter" 
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.

刪除上述文件夾中的內容,一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM