register controls in your web.config file

 
     In previous versions of ASP.NET developers imported and used both custom server controls and user controls on a page by adding directives to the top of pages like so:
<% @ Register TagPrefix="uc" TagName="header" src="Controls/header.ascx"  %>
<% @ Register TagPrefix="uc" TagName="footer" src="Controls/footer.ascx"  %>
<% @ Register Assembly="RssControl" Namespace="WebStudy.Controls" TagPrefix="ctl"  %>


< html >
< form  id ="form1"  runat ="server" >
< uc:header  id ="Myheader"  runat ="server" ></ uc >

</ html >
 
first two register directives above are for user-controls (implemented in .ascx files), while the last is for a custom control compiled into an assembly .dll file.  Once registered developers could then declare these controls anywhere on the page using the tagprefix and tagnames configured.
 
This works fine, but can be a pain to manage when you want to have controls used across lots of pages within your site (especially if you ever move your .ascx files and need to update all of the registration declarations.
 

     ASP.NET 2.0 makes control declarations much cleaner and easier to manage. Instead of duplicating them on all your pages, just declare them once within the new pages->controls section with the web.config file of your application:

<? xml version="1.0" ?>

< configuration >

  
< system .web >
    
    
< pages >
      
< controls >
        
< add  tagPrefix ="uc"  src ="~/Controls/header.ascx"  tagName ="header" />
        
< add  tagPrefix ="uc"  src ="~/Controls/footer.ascx"  tagName ="footer" />
        
< add  tagPrefix ="ctl"  assembly ="RssControl" />
      
</ controls >
    
</ pages >

  
</ system.web >

</ configuration >

 

You can declare both user controls and compiled custom controls this way.  Both are fully supported by Visual Studio

One thing to note above is the use of the "~" syntax with the user-controls.  For those of you not familiar with this notation, the "~" keyword in ASP.NET means "resolve from the application root path", and provides a good way to avoid adding "../" syntax all over your code.  You will always want/need to use it when declaring user controls within web.config files since pages might be using the controls in different sub-directories - and so you always need to resolve paths from the application root to find the controls consistently.

Once you register the controls within the web.config file, you can then just use the controls on any page, master-page or user control on your site like so (no registration directives required):

< html >
< form  id ="form1"  runat ="server" >
< uc:header  id ="Myheader"  runat ="server" ></ uc >

</ html >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值