48

I've made a custom control that inherits from a Literal control. When I try and use my control on a page a parsing error is thrown. I've added this to my web.config

<configuration>
  <system.web>
    <pages>
      <controls>
        <add tagPrefix="one" namespace="myApplication.Controls"/>
      </controls>
    </pages>
  </system.web>
</configuration>

And I've added this to my page

<%@ register namespace="myApplication.Controls" tagprefix="one" %>

Neither of these have fixed the issue. I have a external assembly with some custom controls that work just fine within my project. As a workaround, I'm considering moving my custom control into the external library if there's no simple solution.

--edit

Here's the page code.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SignUp.ascx.cs" Inherits="myApplication.Controls.SignUp" %>
<%@ register namespace="myApplication.Controls" tagprefix="one" %>
<div class="in">
    <span>      
        <one:resourceliteral id="lblFirstname" runat="server" resourcekey="FirstName" resourceresolver="ResourceStringResolver.GetResourceString">
        </one:resourceliteral>      
        </span>
    <div>
        <pl:textbox id="txtFirstName" runat="server"></pl:textbox>
    </div>
</div>

And here's the code for my actual control

namespace myApplication.Controls
{
    public class ResourceLiteral : Literal
    {
        private ResourceManager rm;

        public delegate string dResourceResolver( string label, eLanguage language );

        public event dResourceResolver ResourceResolver;

        public string ResourceKey { get; set; }
        public object DataSource { get; set; }

        private eLanguage _Language = eLanguage.ENUS;
        public eLanguage Language
        {
            get { return _Language; }
            set { _Language = value; }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (ResourceResolver != null)
                Text = ResourceResolver.Invoke( ResourceKey, _Language );
            else
            {
                if(rm != null)
                {
                    Text = rm.GetString( ResourceKey );
                }
            }
        }

        public void LoadDataSource(string resource)
        {
            rm = new ResourceManager( resource, Assembly.GetExecutingAssembly() );
        }

        public void LoadDataSource(Type resource)
        {
            rm = new ResourceManager( resource );
        }
    }
}
Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
Matt
  • 6,264
  • 10
  • 54
  • 82

5 Answers5

86

When adding a namespace, I've found I also need the assembly. If your assembly is also myApplication do this in web.config:

<add tagPrefix="one" namespace="myApplication.Controls" assembly="myApplication"/>

Then, just clean and rebuild and it should all work. Once this is in your web.config, you don't need to add it to your page unless you're using this in a control in the same directory, then you'll need the reference at the top of the web form. But, I recommend against using custom server controls in the same directory as user controls.

Community
  • 1
  • 1
Joseph Yaduvanshi
  • 20,241
  • 5
  • 61
  • 69
  • 3
    That's so strange that it requires the assembly, even though the control is within the app. Thank you for your help. – Matt Jan 05 '10 at 21:57
  • `Once this is in your web.config, you don't need to add it to your page` for me this was the cause of it not working.. thanks – Mr. Blond Nov 11 '20 at 17:56
3

I was receiving the "Unknown server tag" error for a user control that was part of my project. There was no external assembly. @citronas mentioned that "If this does not work, your control probably can't compile.", and that is also listed as the most likely cause in this troubleshooting post.

Although my control code was compiling without errors, it turned out that there were warnings that I was ignoring. My warnings were regarding a resource file that was in my control folder that was referencing another missing file. Once I addressed the warnings, then the control compiled correctly and I was able to use the control with just a Register directive and no modifications to web.config, like this:

<%@ Register TagPrefix="myPrefix" TagName="myControl" Src="~/controls/mySourceFile.ascx" %>

<myPrefix:myControl runat="server"></myPrefix:myControl>
Eric Barr
  • 3,999
  • 5
  • 29
  • 42
2

I also had this problem when publishing my ASP.NET Web Forms application. Even when copying and pasting the folder to the server's IIS, without publishing it, similar problems on pages that used the custom controls / user controls happened systematically.

I registered correctly the controls on web.config and on my dev machine things were working ok. I thought that the registering process was ok.

To fix the problem on publish/copy-and-past deployment process, you should re-register all the user controls's namespaces and assemblies on each page (.aspx) that uses them:

<%@ Register TagPrefix="mycompany" Namespace="MyCompany.Web.Forms.Controls" Assembly="MyCompany.Web" %>
<%@ Register TagPrefix="mycomapny" Namespace="MyCompany.Web.Forms.Controls.ValidatorComponents"  Assembly="MyCompany.Web" %>

Please note that it is same mindset for custom controls or user controls. This issue happened even in VS 2012 but still .NET 4.0. This process is also needed when you ASP.NET skin references such controls.

Mário Meyrelles
  • 1,594
  • 21
  • 26
2

If I understand you correctly, your control is within the same project?

Try to register the control in the markup of your page with the following:

<%@ Register Src="~/controls/foo.ascx" TagName="foo" TagPrefix="uc" %>

With <uc:foo ID="foo1" runat="server"/> you can include the control into the markup. If this does not work, your control probably can't compile. Comment out unnecessary stuff and try it again.

citronas
  • 19,035
  • 27
  • 96
  • 164
  • 3
    It's not a web control that I'm trying to use on my page. It's a custom control that inherits from System.Web.UI.WebControls.Literal class. – Matt Jan 05 '10 at 22:35
0

In my case the application was running in local mode, when publishing got the same error from user control tag.

Upon commenting the user control tag, noticed a path error in another place. Upon fixing the other issue and uncommenting the user control tag, it worked. Seems error displayed is un related.