Register ASP.NET Controls in Web.config

ASP.NET No Comments »

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:

Visual Basic:
  1. <%@ Register assembly="Ektron.Cms.Controls" namespace="Ektron.Cms.Controls" tagprefix="CMS" %>
  2. <%@ Register src="~/webassets/UserControls/WeatherWidget.ascx" tagname="Weather" tagprefix="NeatlySliced" %>
  3. <%@ 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:

XML:
  1. <system.web>
  2. <pages>
  3.       <controls>
  4.         <add tagPrefix="CMS" namespace="Ektron.Cms.Controls" assembly="Ektron.Cms.Controls"/>
  5.         <add src="~/webassets/UserControls/WeatherWidget.ascx" tagName="Weather" tagPrefix="NeatlySliced" />
  6.         <add src="~/webassets/UserControls/Blog.ascx" tagName="Blog" tagPrefix="NeatlySliced" />
  7.       </controls>
  8. </pages>
  9. </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: 16% [?]

CheckBoxList outputs a table by default

ASP.NET 2 Comments »

Building an ASP.NET site, I needed to use a server-side CheckBoxList control. Much to my irritation, the server control output each <input type="checkbox" /> in a tr of a table containing the entire list. I thought, how awful!

Fortunately, I found a way for the server control to quickly output each input in spans rather than table rows: CheckBoxListId.RepeatLayout = RepeatLayout.Flow

The default value is RepeatLayout.Table. Now my checkboxes are cooler.

Popularity: 6% [?]

  • Special Thanks To:Romow Web Directory & WordPress Themes
    Entries RSS Log in