Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Hiding ASP.NET Controls from the Toolbox

On this page:

I've often built custom ASP.NET controls that contain other custom child controls. For example, I have a DataBinder control that contains individual DataBindingItems which are based on Control. The problem is that by default these child controls show up on a control list. Here's what the Visual Studio auto-configured control list looks like for my controls:

The highlighted controls are child controls that shouldn't be there.

This is only an issue if the child items derive from System.Web.UI.Control. If you have child items derived directly from object or Component they don't show up. But if you have control derived children they need to be explicit hidden.

Now, there are lots of attributes you can use to customize how things show up in the control toolbox. But it's not documented very well, and for the longest time I couldn't find the right Attribute that actually hides a control from the toolbox.

The attribute is:

   [ToolboxData("<{0}:wwDataBindingItem runat=server />")]
   [Description("An individual databinding item that allows you to bind a source binding source - a database field or Object property typically - to a target control property")]
   public class wwDataBindingItem : Control

ToolboxItem - I know, I know it seems so damn obvious now. But if you don't know what attribute to use, just try, try to find it . Apparently I'm not the only one. I asked Miguel Castro - the control man - and he too went  "...there's an attribute but hell if I know what it is".

I've looked for this attribute for a while. Eventually I found this by accident as I was poking around some control implementation code with Reflector in System.Web. I was poking around a DataListItem when I noticed the ToolboxItem attribute. Surely I'd seen this attribute before but somehow didn't connect the dots with a logical property to hide the control. Duh!!!

Control development for ASP.NET is one area that is horribly documented on MSDN and there's not really been a good book that I've found that addresses this topic at least not for ASP.NET 2.0. Oh it's all there - somewhere, but scatter over 100 different unrelated pages. It really would be nice if there was one page that lists all the attributes that can be applied to a control, all the attributes that can be applied to a property, to events etc.

Nikhil's book was kind of the standard for ASP.NET 1.1 control development:

Developing Microsoft ASP.NET Server Controls and Components
by Nikhil/Datye, V. Kothari, Nikhil Kothari, Vandana Datye
Microsoft Press (August 28, 2002)


but there's nothing comparable (yet?) for 2.0...

Everytime I'm dealing with attributes on controls it feels like I'm poking around in the dark hoping to get lucky <s>. Oh well, better late than never...

Posted in ASP.NET  Control Development  

The Voices of Reason


June 23, 2007

# re: Hiding ASP.NET Controls from the Toolbox

Hear hear, Rick. If it wasn't for Reflector I wouldn't know how we'd manage to do it at all. In fact, the sooner MS release the source to ASP.NET the better (see ScottGu's comment "I'm hoping/working to try and get the full ASP.NET framework source code published" http://weblogs.asp.net/scottgu/archive/2007/01/30/asp-net-ajax-1-0-source-code-released.aspx#1545238).

Steve from Pleasant Hill
June 23, 2007

# re: Hiding ASP.NET Controls from the Toolbox

Control development and source: that is what made Win32 development such an exciting pastime with Delphi! A number of excellent books were written on this topic (I have an excellent one by Ray Konopka), and since Borland supplied the source code to Delphi reading it solved many a late night problem.

June 23, 2007

# re: Hiding ASP.NET Controls from the Toolbox

Your idea with all the attributes on one place sounds very good. Maybe anybody (to glance at your site) host a Wiki like <a href="http://pinvoke.net/index.aspx">pinvoke.net</a> do it?

Rick Strahl
June 23, 2007

# re: Hiding ASP.NET Controls from the Toolbox

Duncan - yeah source would be nice, but it's no replacement for documentation. Especially for things like Attributes which are difficult to discover even if you have source/Reflector to look at.

June 25, 2007

# re: Hiding ASP.NET Controls from the Toolbox

in fact I knew this attribute (I also found it looking for something else)

But I've never been able to make it working without producing a lot of aside problems eveywhere in my application. So for me it is buggy or a feature, does it?

but ok, I'll try it again on my current project....

Rick Strahl
June 25, 2007

# re: Hiding ASP.NET Controls from the Toolbox

What kind of side features? I actually had this set on a couple of older controls and it doesn't affect operation in any way. It's just a designer attribute so it shouldn't interfere with anything else.

July 26, 2007

# re: Hiding ASP.NET Controls from the Toolbox

Nice hint, it saved me a lot of time, thanks

Imar Spaanjaars
February 19, 2009

# re: Hiding ASP.NET Controls from the Toolbox

Hi Rick,

It has taken me 1.5 years to get started, but I created the site you were wishing you had 1.5 years ago: http://www.dotnetattributes.com

The site contains all the attributes found in .NET 3.5, together with assembly references, namespaces, usage (like properties, classes, events, methods and so on), documentation and more. On top of that, it's open for the community so hopefully other developers will help enrich the data.

It's been mainly a pet project, based on your old wish here, but it was fun to do. For more info: http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=487

Hope you enjoy it, even though it's a bit late.... ;-)



West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024