web developer tips (9):在Web.config文件中注册web用户控件

原文地址:How to create an ASP.NET Web User Control and include it in your web page

在上一篇创建web用户控件并包含在web页面里中介绍了如何创建一个web用户控件和在页面使用它。注意:在页面拖拽一个web用户控件后,VS 会在页面上方添加 @Register指令。

http://www.watch-life.net/visual-studio/register-your-asp-net-web-user-controls-in-web-config.html

如果将来移动了web用户控件的位置的话,那么在每个使用了这个web用户控件的页面都需要修改注册指令,这是件很麻烦的事,如果把注册指令移到web.config文件里,那么修改文件的路径就会变得容易多了,任何位置改变更新在一个位置就可以完成。在Web.Config里进行如下输入:


<configuration>
<system.web>
<pages>


<controls>
<add tagPrefix="MyControl" src="~/Controls/WebUserControl.ascx"


tagName="MyButton"/>
</controls>


</pages>
</system.web>
</configuration> 

 

 


 

一旦web config如上述配置后,就可以在(项目)的任何页面来使用这个web用户控件了。


<body>
<form id="form1" runat="server">
<MyControl:MyButton ID="MyUserControl" runat="server" />
</form>
</body>

 

 

 

以前还真不知道web用户控件居然可以在Asp.net 的web.config里面注册,这个功能设计很很人性化,考虑真周到。

更多文章见:守望轩[http://www.watch-life.net/]

posted on 2009-06-19 09:19  xjb  阅读(342)  评论(0编辑  收藏  举报

导航