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

Mat*_*att 42 c# asp.net custom-controls

我已经制作了一个继承自Literal控件的自定义控件.当我尝试在页面上使用我的控件时,会抛出解析错误.我已将此添加到我的web.config中

<configuration>
  <system.web>
    <pages>
      <controls>
        <add tagPrefix="one" namespace="myApplication.Controls"/>
      </controls>
    </pages>
  </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我已将此添加到我的页面中

<%@ register namespace="myApplication.Controls" tagprefix="one" %>
Run Code Online (Sandbox Code Playgroud)

这些都没有解决这个问题.我有一个带有一些自定义控件的外部程序集,可以在我的项目中正常工作.作为一种解决方法,如果没有简单的解决方案,我正在考虑将我的自定义控件移动到外部库中.

- 编辑

这是页面代码.

<%@ 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>
Run Code Online (Sandbox Code Playgroud)

这是我实际控制的代码

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 );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jim*_*ert 77

添加命名空间时,我发现我还需要程序集.如果您的程序集也在myApplicationweb.config中执行此操作:

<add tagPrefix="one" namespace="myApplication.Controls" assembly="myApplication"/>
Run Code Online (Sandbox Code Playgroud)

然后,只需清理和重建,它应该都可以工作.一旦这在你的web.config中,你不需要将它添加到你的页面,除非你在同一目录的控件中使用它,那么你需要在Web表单顶部的引用.但是,我建议不要在与用户控件相同的目录中使用自定义服务器控件.

  • 这很奇怪,它需要组装,即使控件在应用程序内.谢谢您的帮助. (3认同)
  • 我明白为什么这个答案有这么多的选票. (3认同)