zoukankan      html  css  js  c++  java
  • Tip #4 ASP.NET用户控件也可以在Web.Config中进行引用

     

     我们在引用用户控件时,如果引用控件的文件很多,那么经常要每个页面都加上Register 指令,真是痛苦。

    还好,我们可以在web.config里注册控件:

    1. <configuration>
    2.   <system.web>
    3.     <pages>
    4.        <controls>
    5.          <add tagPrefix="MyControl" src="~/Controls/WebUserControl.ascx" tagName="MyButton"/>
    6.        </controls>
    7.     </pages>
    8.   </system.web>
    9. </configuration>

    同样的,第三方控件也可以在这里注册

    1. <configuration>
    2.   <system.web>
    3.     <pages>
    4.        <controls>
    5.          <add tagPrefix="ControlVendor" assembly="ControlVendorAssembly"/>
    6.        </controls>
    7.     </pages>
    8.   </system.web>
    9. </configuration>

    需要注意的是,上面用户控件中“~”句法的使用。对那些不熟悉这个符号的人,ASP.NET中“~”符号意思是“从应用的根路径来定位”,它提供了一个很好的方法来避免在你的编码里到处使用“..”。在web.config文件里声明用户控件时,你总是应该使用它,因为页面也许会使用在不同子目录里的控件,所以你应该总是始终如一地从应用的根路径开始定位这些控件。


    一旦你在web.config 文件中声明好这些控件后,你就可以在你网站上的任何一个页面,母板页或者用户控件中使用它们了,象这样(不再需要注册指令):

    1. <html>
    2. <body>
    3.     <form id="form1" runat="server">
    4.         <MyControl:MyButtonIDMyControl:MyButtonID="MyButton1" runat="server" /> 
    5.     </form>
    6. </body>
    7. </html>

     

  • 相关阅读:
    python字符串格式化
    MFC----任务管理器的制作
    高斯消元方程组
    linux qq下载
    python——tuple元组
    Codeforces 515C. Drazil and Factorial
    HDU 1102 Constructing Roads (最小生成树)
    hdu 01背包汇总(1171+2546+1864+2955。。。
    HDU 3392 Pie(DP)
    HDU 1024
  • 原文地址:https://www.cnblogs.com/honker/p/3774458.html
Copyright © 2011-2022 走看看