ASP.NET自定义控件 - 未知的服务器标记 [英] ASP.NET Custom Control - Unknown server tag
问题描述
我做了从文字控制继承的自定义控制。当我尝试并使用网页分析错误会被抛出我的控制。我已经添加到了我的web.config
<结构>
<&的System.Web GT;
<网页和GT;
<控制与GT;
<添加标签preFIX =一命名空间=myApplication.Controls/>
< /控制>
< /页>
< /system.web>
< /结构>
和我添加这到我的网页
<%@注册的命名空间=myApplication.Controls标签preFIX =一字%GT;
这些都不是固定的问题。我有我的项目中工作得很好,一些自定义的控件的外部组件。作为一种变通方法,我在考虑我的移动定制控件到外部库如果没有简单的解决方案。
- 修改
下面的页面code。
<%@控制语言=C#AutoEventWireup =真codeBehind =SignUp.ascx.cs继承=myApplication.Controls.SignUp%GT ;
<%@注册的命名空间=myApplication.Controls标签preFIX =一字%GT;
<中的&GT DIV CLASS =;
<跨度>
< 1级:resourceliteral ID =lblFirstname=服务器的ResourceKey =姓resourceresolver =ResourceStringResolver.GetResourceString>
< /之一:resourceliteral>
< / SPAN>
< DIV>
< PL:文本ID =txtFirstName=服务器>< / PL:文本>
< / DIV>
< / DIV>
和这里的code为我的实际控制权
命名空间myApplication.Controls
{
公共类ResourceLiteral:文字
{
私人ResourceManager的RM; 公共委托串dResourceResolver(字符串标签,eLanguage语言); 公共事件dResourceResolver ResourceResolver; 公共字符串的ResourceKey {搞定;组; }
公共DataSource对象{搞定;组; } 私人eLanguage _Language = eLanguage.ENUS;
公共eLanguage语言
{
{返回_Language; }
集合{_Language =价值; }
} 保护覆盖无效的OnLoad(EventArgs的发送)
{
base.OnLoad(E); 如果(ResourceResolver!= NULL)
文字= ResourceResolver.Invoke(的ResourceKey,_Language);
其他
{
如果(RM!= NULL)
{
文字= rm.GetString(获取ResourceKey);
}
}
} 公共无效LoadDataSource(字符串资源)
{
RM =新的ResourceManager(资源,Assembly.GetExecutingAssembly());
} 公共无效LoadDataSource(资源类型)
{
RM =新的ResourceManager(资源);
}
}
}
在添加一个命名空间,我发现我还需要装配。如果您总成也所有MyApplication
在web.config中做到这一点:
<添加标签preFIX =一命名空间=myApplication.Controls集结号=MyApplication的/>
然后,只需清洁和重建,它应该所有的工作。一旦这是你的web.config,你不需要,除非你使用这种在同一目录中的控制,那么你就需要在Web窗体顶部的参考,将其添加到您的网页。但是,我建议对在同一个目录的用户控件使用自定义服务器控件。
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 );
}
}
}
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.
这篇关于ASP.NET自定义控件 - 未知的服务器标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!