Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

c# - ASP.NET Custom Control - Unknown server tag

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 );
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...