Asp.net
如何在 ASP.NET 頁面上註冊自定義伺服器控制項
我有一個項目,我正在嘗試在頁面上註冊一個自定義伺服器控制項(沒有
.ascx文件)。我目前正在使用類聲明
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’”。**
我究竟做錯了什麼?
好吧,如果這個控制項在另一個類庫中,或者即使它在同一個類庫中,在@Register 中指定控制項的程序集也不是一個壞主意:
<%@ Register TagPrefix="myControls" Namespace="MyApp.Controls" Assembly="MyApp" %> <myControls:CustomControl runat="server" Text="What's up!" />清理並重建您的解決方案,以驗證所有內容均已正確編譯!