R – ASP.NET – Placement of @ Register directive

asp.netcoding-styledirectiveuser-controls

In the past I have always placed my <%@ Register … %> directive(s) at the top of my .aspx pages just below the @ Page directive. I recently found out that I can place this register directive ANYWHERE in the .aspx page and still have it function correctly. We are wondering if there is any problem people can foresee with placing these right above the first instance of a user control, for example:

<%@ Page .. %>

<div>
<asp:TextBox ..>
...
...
<%@ Register src="~/UserControls/UserControl.ascx" ..>
<uc1:UserControl ..>
...
</div>

If we do it this way, it makes it a lot easier to copy and paste user controls from one page to another. Are there any disadvantages to this style?

Best Answer

Typically this is not good practice since you could have multiple UserControls on a page. I would continue to do it at the top or if it is application-wide consider placing it in your web.config file:

http://weblogs.asp.net/scottgu/archive/2006/11/26/tip-trick-how-to-register-user-controls-and-custom-controls-in-web-config.aspx

Related Topic