Make your string property a servercontrol dropdownlist
Often when developing custom server controls, you want a property to be a reference to another control on the page. The validator controls do this with a string property that is the control's ID. But they do something nice, where they show a dropdownlist in the propertygrid editor that lists all the controls which can be validated. Many people want to do a similar thing, but can't figure out how to do it. The key is that you want to use a custom TypeConverter. Here's a handy base class that you can either use outright, or customize appropriately, as a TypeConverter that shows a list of the server controls on the page.
using System;
using System.Collections;
using System.ComponentModel;
using System.Web.UI;
namespace ControlPicker {
public class ServerControlConverter : StringConverter {
#region Make It A ComboBox
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
return true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) {
return false;
}
#endregion
#region Display Control IDs In List
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
if ((context == null) || (context.Container == null)) {
return null;
}
Object[] serverControls = this.GetControls(context.Container);
if (serverControls != null) {
return new StandardValuesCollection(serverControls);
}
return null;
}
private object[] GetControls(IContainer container) {
ArrayList availableControls = new ArrayList();
foreach( IComponent component in container.Components ) {
Control serverControl = component as Control;
if ( serverControl != null &&
!(serverControl is Page) &&
serverControl.ID != null &&
serverControl.ID.Length != 0 &&
IncludeControl(serverControl)
) {
availableControls.Add(serverControl.ID);
}
}
availableControls.Sort(Comparer.Default);
return availableControls.ToArray();
}
#endregion
//Override this method to customize which controls show up in the list
protected virtual Boolean IncludeControl( Control serverControl ) {
return true;
}
}
}