在Web.Config註冊控件

摘要:在WebConfig註冊控件

 ASP.NET 2.0以前,必須都要在每頁的頂部加上<%@ Register %>來註冊控件(自訂UserControl或Server Control),現ASP.NET 2.0以後不用在每頁都要註冊那麼麻煩,只要在Web.Config設定一下就可以了。

1.註冊usercontrol:

Before:
 
.aspx檔:
<%@ Register TagPrefix="UserTag" TagName=" Banner" Src="~/Controls/ Banner.ascx" %>
<html>
<body>
    <form id="form1" runat="server">
        < UserTag:Banner ID=" Banner1" runat="server" />
    </form>
</body>
</html>

 

 After:

Web.cofig檔案:
<configuration>
  <system.web>
    <pages>
      <controls>
       <add tagPrefix="UserTag" tagName=" Banner" src="~/Controls/ Banner.ascx" />
      </controls>
    </pages>
  </system.web>
</configuration>

 .aspx檔:

<html>
<body>
    <form id="form1" runat="server">
        < UserTag:Banner ID=" Banner1" runat="server" />
    </form>
</body>
</html>

 

2.如果是註冊組件:
 
Web.cofig檔案:
<add tagPrefix=" Test" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=2.0.0.1, Culture=neutral, PublicKeyToken= 5298A1BD3421ECDC"/>

 .aspx檔案:

<html>
<body>
    <form id="form1" runat="server">
        < Test:TextBox ID="txtName" runat="server"></ Test:TextBox>
    </form>
</body>
</html>