将asp.net usercontrol(用户控件页)转变为普通控件

来源:互联网 发布:linux五笔输入法下载 编辑:程序博客网 时间:2024/04/30 13:44

步骤:
1. 建立一个WEB站点工程,创建一个ascx控件页,再创建一个aspx页面,将ascx放到aspx页面上,保证程序能正常运行;
2. 删除aspx文件, 点build->publish web site菜单, 在点出的窗口中: 取消选择:Allow this precompiled to be updatable,这样ascx页面的内容将会被编译到DLL中.
3. 选中Use fixed naming and single page assemblies, 这样只会生成一个DLL文件.
编译之后即可生成一个DLL.

然后新建一个WEB工程, 添加对这个DLL的引用, 在aspx页面顶端添加: <%@Register TagPrefix="mycontrol" Namespace="WebUserControl" Assembly="App_Web_helloworld.ascx.cdcab7d2"%>
具体的参数与创建ASCX工程和生成的DLL有关.

然后在页面中添加一行: <mycontrol:helloworld id="MyUC" runat="server"/>
此时在此aspx页面上即可使用先前建立的ASCX页面.


为了能在所有aspx中使用此ASCX控件,而不需要在每个页面的顶端进行声明,可以在web.config中如下声明
<system.web>
  <pages theme="Default">
   <controls>
    <add tagPrefix="myconrol" namespace="WebUserControl" assembly="App_Web_helloworld.ascx.cdcab7d2"/>
   </controls>
  </pages>