Tips about Custom ToolPart[] to keep your properties

on August 7, 2006


I decided to demonstrate the use of the ToolPart in a custom Web Part (SPS2003).

Note that this article is intended for beginners.

Sometimes, you don’t need to add 200 lines of code to help in your day to day web parts administration. This tip is a good example of this.

When you design a new web part, if you create a Custom Property for your web part, it shows in the “Miscellaneous” category, at the end of all the categories. Worse, the category containing the important properties is collapsed…

To quickly install and set the properties of a new web part, you need almost the opposite:

Have the new category appear first, Set the name of the categories to anything else than “Miscellaneous” and why not override some default settings in the existing categories fro the ToolPane?

In the provided code, you override ToolPart[].

This code can be pasted as it is in your web part main class.

In the following example, any custom property you add only needs to refer to the category “Custom Properties” to be included in the new ToolPart (Look for “Category("Custom Properties")“ in the code example).

What is useful to know about this code is the following:

toolparts[0] = custom; means that you place the new ToolPart on top of the others in the Tool Pane. Simple, the first item in the array appear on top…

wptp.Expand(WebPartToolPart.Categories.Appearance); and custom.Expand("Custom Properties"); will expand the corresponding categories. This is very useful as it removes the need to click to expand you main category. This way, as soon as the Tool Pane appears, you have access to the properties.

Tip in a Tip

Here is another feature I use in many custom web parts.

There are different ways to override the general properties of your web part. For some web parts it is obvious that you do not want to have frame nor the title bar. You can force the web part to not display only the output created by your code. To do so, you use: “this.FrameType = FrameType.None;” It can be used in different places;

The one I prefer is the following:

#region Override CreateChildControls() protected override void CreateChildControls() {       this.FrameType = FrameType.None; } #endregion

It Overrides the ASP.NET Web.UI.Controls.CreateChildControls method to create the objects for the Web Part’s controls.
From this overridden method you can add controls and… set properties.