C# ASP.NET 自定义控件 - 未知的服务器标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2009138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:37:58  来源:igfitidea点击:

ASP.NET Custom Control - Unknown server tag

c#asp.netcustom-controls

提问by Matt

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

我制作了一个继承自 Literal 控件的自定义控件。当我尝试在页面上使用我的控件时,会引发解析错误。我已将此添加到我的 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 );
        }
    }
}

采纳答案by Jim Schubert

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

添加命名空间时,我发现我还需要程序集。如果您的程序集也在myApplicationweb.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.

然后,只需清理和重建,它应该一切正常。一旦它在您的 web.config 中,您就不需要将它添加到您的页面,除非您在同一目录中的控件中使用它,然后您将需要 Web 表单顶部的引用。但是,我建议不要在与用户控件相同的目录中使用自定义服务器控件。

回答by citronas

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.

有了<uc:foo ID="foo1" runat="server"/>可以包括控制到标记。如果这不起作用,您的控件可能无法编译。注释掉不必要的东西,然后再试一次。

回答by Mário Meyrelles

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.

我在发布 ASP.NET Web 窗体应用程序时也遇到了这个问题。即使在将文件夹复制并粘贴到服务器的 IIS 时,没有发布它,使用自定义控件/用户控件的页面上也会系统地发生类似的问题。

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.

我在 web.config 和我的开发机器上正确注册了控件,一切正常。我以为注册过程没问题。

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:

要解决发布/复制和过去部署过程中的问题,您应该在使用它们的每个页面 (.aspx) 上重新注册所有用户控件的命名空间和程序集:

<%@ 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.

请注意,自定义控件或用户控件的思维方式相同。即使在 VS 2012 中也会发生此问题,但仍然是 .NET 4.0。当 ASP.NET 外观引用此类控件时,也需要此过程。

回答by Eric Barr

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.

我收到了属于我的项目的用户控件的“未知服务器标记”错误。没有外部组装。@citronas 提到“如果这不起作用,您的控件可能无法编译。”,并且在此故障排除帖子中也将其列为最可能的原因。

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 指令而不修改 web.config 来使用控件,如下所示:

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

<myPrefix:myControl runat="server"></myPrefix:myControl>

回答by Ramesh Venkataswamy

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.

在评论用户控件标记时,注意到另一个地方的路径错误。在修复另一个问题并取消注释用户控件标记后,它起作用了。似乎显示的错误无关。