为全站网页添加全局“变量”

Register controls and import namespace for all pages globaly
为网站所有的页面都注册控件和导入命名空间


Something that could be annoying when creating custom controls for Asp.net2.0 is to always add the register directive to the page for adding a reference to the custom control so it could be used on the page.Another thing that could also be annoying is to add the import directive to each page when we want to us a specific namespace.If you know that you are going to use a custom control on most of the pages or use a namespace, you can instead of using the register and namespace directive use the pages section's namespace and controls section in the web.config file.

当我们在asp.net2.0中创建自定义控件的时候,经常要在页面添加自定义控件的引用才能使用它们,这是非常烦恼的事情。同样,在我们使用特定的命名空间的时候,也需要在每个页面导入命名空间。如果我们知道在页面上创建自定义控件和导入命名空间的方法,那可以在web.config这个文件上代替它们。

The following code will add the namespace System.Data.SqlClient to it could be used from all of the pages:
接下来的代码将把命名空间system.data.sqlclient添加到配置文件中,以在全部的页面上使用:

<system.web>
  <pages>
    <namespaces>
      <add namespaces="System.Data.SqlClient"/>
    </namespaces>
  </pages>
</system.web>

Here is a list of namespaces that are already added to the namespace section internally, and will be available for all the pages.
这里是一些命名空间,它们可以添加到配置文件中,对全部的页面都有效。
System
System.Collections
System.Collections.Specialized
System.Configuration
System.Text
System.Text.RegularExpressions
System.Web
System.Web.Caching
System.Web.SessionState
System.Web.Security
System.Web.Profile
System.Web.UI
System.Web.UI.WebControls
System.Web.UI.WebControls.WebParts
System.Web.UI.HtmlControls

The following code will make a custom control available for all of the pages:
接下来的代码将显示一个自定义控件在所有的页面中都有效:
<system.web>
  <pages>
    <controls>
      <add tagPrefix="MyControl" namespace="MyControls"/>
    </controls>
  </pages>
</system.web>

If you have the MyControls in the Application_code folder,you only need to specify the namespace.If it's located within an assembly, you have to specify the assembly too,by using the Assembly attribute:
如果在程序代码文件有MyControls控件,那只需要指定它的命名空间。如果它被指向个组件的话,那必需使用组件属性:
<add tagPrefix="MyControls" namespace="MyControls" Assemble="MyAssembly"/>

You can also register a user control for all of the pages:
你还能注册个用户控件,以使它在全部页面有效:
<add tagPrefix="MyUserControl" tagname="MyControl" src="MyControl.ascx"/>

摘自http://fredrik.nsquared2.com
这篇文章是我第一次翻译,不好的地方,还请高手们多多指教!(微软的脚步太快,自己还没有用熟asp.net1.1呢,就出来了个asp.net2.0)

posted @ 2005-01-30 17:03  PeterLin88  阅读(1437)  评论(0编辑  收藏  举报