Friday, April 21, 2006

Design-time goodness: ImageUrls

When you're developing web server controls, how often do you expose a Url or an ImageUrl property? Chances are that it's one of those tasks that happens 9 times out of 10, as adding image support to an existing or composite control is that common a task.

In this forum post, Steven Cheng from MS support gives a good code-snippet for a property that has the default ImageUrl designer applied to it. Unfortunately, he neglects to point out the assembly references needed to get it to work.

To use the standard designers requires you to reference the System.Design.dll assembly in your control library, and then to add the using statements highlighted in the sample below


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Design;
using System.Drawing.Design;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;

namespace DemoControls
{
[DefaultProperty("Caption")]
[ToolboxData("<{0}:CoolButton runat=server>")]
public class CoolButton : ImageButton
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
[Editor("System.Web.UI.Design.ImageUrlEditor, System.Design,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",typeof(UITypeEditor))]
public string RolloverImageUrl
{
get
{
String s = (String)ViewState["RolloverImageUrl"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["RolloverImageUrl"] = value;
}
}

// rest of the implementation goes here...
}


Adding this kind of design-time support is a snip, and makes those simple extended controls much more usable.

No comments: