Andy Smith's Blog

Page.RegisterStartupScript('Andy', 'MetaBuilders_WebControls_GainKnowledge();');

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;
  }
 }
}

Comments

Matt Hawley said:

Awesome! I've been wondering how to do this for my Ordered List Box control. Now I can!
# September 4, 2003 5:16 PM

Fred said:

Exactly what I was looking for! Thank you!
# January 5, 2004 6:50 AM

TrackBack said:

# February 7, 2004 2:15 AM

TrackBack said:

# February 7, 2004 2:47 AM

Agustín González said:

Perfect! I was looking for this. Thank you very much.
# March 15, 2004 7:33 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)