User Controls in ASP.NET are a great tool - they allow you to consolidate sections of code into a reusable piece that you can use anywhere on your site - or even copy the control to a different site.
The only issue I had with user controls was that you had to register the code on every.single.page that you wanted to use it on before you could use it. So, that meant something like this at the top of every.single.page:
-
<%@ Register assembly="Ektron.Cms.Controls" namespace="Ektron.Cms.Controls" tagprefix="CMS" %>
-
<%@ Register src="~/webassets/UserControls/WeatherWidget.ascx" tagname="Weather" tagprefix="NeatlySliced" %>
-
<%@ Register src="~/webassets/UserControls/Blog.ascx" tagname="Blog" tagprefix="NeatlySliced" %>
Oi! The frustration! There has to be a better way!
Some googling resulted in finding the wondeful haacked.com, with the article listing my answer. ASP.NET 2.0 allows for registering controls in web.config, thus making it available for all pages and eliminating a potential very long list of controls on every page.
The listings go inside the <controls> tag, within the <pages> section, such as follows:
-
<system.web>
-
<pages>
-
<controls>
-
<add tagPrefix="CMS" namespace="Ektron.Cms.Controls" assembly="Ektron.Cms.Controls"/>
-
<add src="~/webassets/UserControls/WeatherWidget.ascx" tagName="Weather" tagPrefix="NeatlySliced" />
-
<add src="~/webassets/UserControls/Blog.ascx" tagName="Blog" tagPrefix="NeatlySliced" />
-
</controls>
-
</pages>
-
</system.web
You'll note I have Ektron listed in there as well. This is because I was tired of adding the Controls assembly to every page - so not only can you add your own user controls, but assemblies as well. What a life-saver! Your resulting code does not change at all, but you have just eliminated that page heading overhead.
Have you found another item to register in web.config? Leave a comment and share the wealth!
Popularity: 18% [?]








