views:

110

answers:

2

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?

+1  A: 

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

Josh Barker
Thanks for the reply - this seems like a good solution for user controls that we want to be able to use in more than one page.
Keith
A: 

See the MSDN reference on Directive Syntax

Excerpt:

When used, directives can be located anywhere in an .aspx or .ascx file, though standard practice is to include them at the beginning of the file. Each directive can contain one or more attributes (paired with values) that are specific to that directive.

And useful info for @Register.

o.k.w
Yes, I read that, and it doesn't noticeably mention any disadvantages to placing the @ Register directives wherever you want.
Keith