如何注册ASP.NET页面上自定义服务器控件 [英] How to register custom server control on ASP.NET page
本文介绍了如何注册ASP.NET页面上自定义服务器控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个项目,我想注册一个自定义服务器控件(没有的.ascx
文件),在页面上。我目前使用
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
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);
}
}
}
在我的网页,
<%@ Register TagPrefix="myControls" Namespace="MyApp.Controls" %>
<myControls:CustomControl runat="server" Text="What up!" />
我收到一个分析器错误,消息未知的服务器标记myControls:CustomControl'。
I receive a Parser Error, with the message "Unknown server tag 'myControls:CustomControl'."
我在做什么错了?
推荐答案
好吧,如果这个控制是在另一个类库,或者即使是在同一个,它不会是一个坏主意来指定控件的程序@Register:
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 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!
这篇关于如何注册ASP.NET页面上自定义服务器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文