Tips and tricks: How to Register User Controls and custom controls in Web. config
[Original address] tip/TRICK: how to register user controls and custom controls in Web. config
[Original article published on] Sunday, November 26,200
Last year, I used this technique in my asp. net skills/know-how, but if so many people are always surprised by its existence, I think it is worth using an exclusive tip/tip post to improve its visibility (Click here to read my asp. net Tips/other posts in the relevant series ).
Problem:
In earlier versions of ASP. NET, developers Add the <% @ register %> command at the top of the page to introduce and use custom server controls and user controls, as shown in the following figure:
<% @ Register tagprefix = "Scott" tagname = "Header" src = "controls/header. ascx" %>
<% @ Register tagprefix = "Scott" tagname = "footer" src = "controls/footer. ascx" %>
<% @ Register tagprefix = "controlvendor" assembly = "controlvendor" %>
<HTML>
<Body>
<Form ID = "form1" runat = "server">
<SCOTT: Header id = "myheader" runat = "server"/>
</Form>
</Body>
</Html>
Note that the first two registration commands above are used to register User Controls (implemented in the. ascx file), and finally this is used to register and compileProgramSet. dll file. After registration, developers can declare these controls with the configured tagprefix and tagname anywhere on the page.
This works well, but it is very painful to manage it. When you want to use controls on many pages of your website, especially if you move it. to update all registration declarations in the ascx file.
Solution:
ASP. NET 2.0 makes the control declaration extremely clean and easy to manage. You do not need to repeat these declarations on your page, as long as you declare them once in the new pages-> controls section of the web. config file of your application:
<? XML version = "1.0"?>
<Configuration>
<System. Web>
<Pages>
<Controls>
<Add tagprefix = "scottgu" src = "~ /Controls/header. ascx "tagname =" Header "/>
<Add tagprefix = "scottgu" src = "~ /Controls/footer. ascx "tagname =" footer "/>
<Add tagprefix = "controlvendor" assembly = "controlvendorassembly"/>
</Controls>
</Pages>
</System. Web>
</Configuration>
You can declare the user control and the compiled custom control at the same time in this way. When you use this technique, Visual Studio fully supports both of them, and both the VS 2005 web site project and the VS 2005 web application project support both. Visual Studio displays these controls in WYSIWYG mode in the designer, and displays the declaration of the control field in the background encoding file.
Note that "~" in the above User Control Syntax. For those who are not familiar with this symbol, "~" in ASP. NET The symbol means "locating from the application root path". It provides a good way to avoid using ".. \" everywhere in your code. In the web. when declaring a user control in the config file, you should always use it, because the page may use controls in different subdirectories, therefore, you should always locate these controls from the root path of the application.
Once you are on the web. after these controls are declared in the config file, you can use them on any page, dashboard page, or user control on your website, as shown in the following figure (you no longer need to register commands ):
<HTML>
<Body>
<Form ID = "form1" runat = "server">
<Scottgu: Header id = "myheader" runat = "server"/>
</Form>
</Body>
</Html>
I hope this article will help you,
Scott
Note: I am particularly grateful to Phil haack for discussing this technique in his blog earlier this month. For those of you who don't know Phil, he helped build a very popular subtext blog engine and had a wonderful blog.