I have a project and I am trying to register a custom server control (there is no .ascx
file) on the page. I am currently using
我有一个项目,我试图在页面上注册自定义服务器控件(没有.ascx文件)。我目前正在使用
Class Declaration
namespace MyApp.Controls{
public class CustomControl: WebControl{
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(Text);
}
}
}
On my page,
在我的页面上,
<%@ Register TagPrefix="myControls" Namespace="MyApp.Controls" %>
<myControls:CustomControl runat="server" Text="What up!" />
I receive a Parser Error, with the message "Unknown server tag 'myControls:CustomControl'."
我收到一个Parser Error,消息“Unknown server tag'myControls:CustomControl'。”
What am I doing wrong?
我究竟做错了什么?
4 个解决方案
#1
41
Well, if this control is in another class library, or even if it's in the same one, it wouldn't be a bad idea to specify control's assembly in @Register:
好吧,如果这个控件在另一个类库中,或者即使它在同一个库中,那么在@Register中指定控件的程序集也不是一个坏主意:
<%@ Register TagPrefix="myControls" Namespace="MyApp.Controls" Assembly="MyApp" %>
<myControls:CustomControl runat="server" Text="What's up!" />
Clean and rebuild your solution too in order to verify everything is compiled rightly!
清理并重建您的解决方案,以验证所有内容是否正确编译!
#2
7
If your control will be reused on several pages, you may want to register it in web.config, as one of system.web/pages/controls subelements instead of copy-pasting the same <@Register tag in all affected pages.
如果您的控件将在多个页面上重用,您可能希望在web.config中注册它,作为system.web / pages / controls子元素之一,而不是在所有受影响的页面中复制粘贴相同的<@Register标记。
web.config:
web.config中:
<system.web>
<pages ...>
<controls>
...
<add tagPrefix="myCompany" namespace="MyCompany.Whatever.Controls" assembly="Whatever"/>
</controls>
thepage.aspx:
thepage.aspx:
<myCompany:ControlClassName ID="TheStuff" runat="server" ... />
#3
5
You should put your control either under the App_Code
folder (in the case if the control not in assembly) or add a reference to assembly where this control is:
您应该将控件放在App_Code文件夹下(如果控件不在程序集中),或者添加对此控件所在的程序集的引用:
<%@ Register TagPrefix="myControls" Namespace="MyApp.Controls"
Assembly="SomeAssembly" %>
But guessing, your control not under the App_Code
folder.
但猜测,你的控件不在App_Code文件夹下。
#4
4
Add an assembly attribute to your register tag
将程序集属性添加到注册标记
#1
41
Well, if this control is in another class library, or even if it's in the same one, it wouldn't be a bad idea to specify control's assembly in @Register:
好吧,如果这个控件在另一个类库中,或者即使它在同一个库中,那么在@Register中指定控件的程序集也不是一个坏主意:
<%@ Register TagPrefix="myControls" Namespace="MyApp.Controls" Assembly="MyApp" %>
<myControls:CustomControl runat="server" Text="What's up!" />
Clean and rebuild your solution too in order to verify everything is compiled rightly!
清理并重建您的解决方案,以验证所有内容是否正确编译!
#2
7
If your control will be reused on several pages, you may want to register it in web.config, as one of system.web/pages/controls subelements instead of copy-pasting the same <@Register tag in all affected pages.
如果您的控件将在多个页面上重用,您可能希望在web.config中注册它,作为system.web / pages / controls子元素之一,而不是在所有受影响的页面中复制粘贴相同的<@Register标记。
web.config:
web.config中:
<system.web>
<pages ...>
<controls>
...
<add tagPrefix="myCompany" namespace="MyCompany.Whatever.Controls" assembly="Whatever"/>
</controls>
thepage.aspx:
thepage.aspx:
<myCompany:ControlClassName ID="TheStuff" runat="server" ... />
#3
5
You should put your control either under the App_Code
folder (in the case if the control not in assembly) or add a reference to assembly where this control is:
您应该将控件放在App_Code文件夹下(如果控件不在程序集中),或者添加对此控件所在的程序集的引用:
<%@ Register TagPrefix="myControls" Namespace="MyApp.Controls"
Assembly="SomeAssembly" %>
But guessing, your control not under the App_Code
folder.
但猜测,你的控件不在App_Code文件夹下。
#4
4
Add an assembly attribute to your register tag
将程序集属性添加到注册标记