Registering custom controls fails

前端 未结 1 631
生来不讨喜
生来不讨喜 2021-01-21 04:06

I am attempting to register my user controls within the webconfig file because I am receiving the Element does not exist error, but I am receiving the following error when I try

相关标签:
1条回答
  • 2021-01-21 04:33

    Get rid of the namespace attribute, as it's confusing ASP.NET as to whether you're attempting to register a User Control or a Custom Control.

    User Control:

    <add tagPrefix="SomeTagPrefix" src="~/Controls/SomeControl.ascx" tagName="SomeTagName"/>
    

    Custom Control:

    <add tagPrefix="SomeTagPrefix" namespace="SomeNamespace" assembly="SomeAssembly"/>
    

    So, in your example:

    <add tagPrefix="IPAMControl" tagName="track" src="~/controls/track.ascx" />
    

    And on the ASPX/ASCX, you use it like this:

    <IPAMControl:track id="ipamTrack" runat="server" />
    

    See here for more info.

    EDIT

    To prove this works - i did the following:

    1. Create new Web Application
    2. Create new folder called "Controls" in the root of the web application
    3. Added a new "Web User Control" called "MyUserControl.ascx"
    4. Modified web.config to add registration of control
    5. Modified Default.aspx to add the control.

    And it all works fine.

    Here is the User Control:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs" Inherits="WebApplication1.Controls.MyUserControl" %>
    <span>Hi, im a user control, how are you?</span>
    

    Here is the portion of the web.config i edited:

    <pages>
          <controls>
            <add tagPrefix="MyControls" tagName="MyUserControl" src="~/Controls/MyUserControl.ascx"/>
          </controls>
        </pages>
    

    Here is the Default.aspx change i made:

    <MyControls:MyUserControl id="myUserControl" runat="server" />
    

    And the page rendered correctly.

    Now, unless what i have done here is different to how you have attempting to do it, you must have other code/errors that is interfering with this.

    Don't know how much else help i can be.

    0 讨论(0)
提交回复
热议问题