The AssociatedControlConverter is cool to use when building ASP.NET Web Controls. The MSDN help for the AssociatedControlConverter
describes its function as "Provides a type converter that retrieves a list of WebControl controls in the current container".
Translated this means when used in a custom control you can provide designer support for assigning the ID of another control on the form to the property of your custom control. The ControlToValidate property on ASP.NET validator controls provides a good illustration of its function.
When in the designer, the ControlToValidate property has a drop down in which you can select the TextBox to be validated.
You can create similar properties on your own controls as follows:-
/// <summary>
/// Gets or sets the label in which to display a logical question
/// </summary>
[Category("Behavior"),
DefaultValue(""),
Description("Gets or sets the label in which to display the question."),
TypeConverter(typeof(AssociatedControlConverter))]
public string QuestionControl
{
get
{
return _questionControlID;
}
set
{
_questionControlID = value;
}
}
If you use this code the first thing you'll notice is that when you inspect the property of your control in the designer, the drop down
doesn't just contain a list of textboxes as in the ControlToValidate
property on validator controls, it has a list of all the controls on the web form inheriting from WebControl.
Sometimes this isn't desirable, and as in the case of the Validator control you really only want people selecting from a list of textboxes on the form or other types of controls, such as a Hyperlinks, Buttons, or Labels.
The answer is in the FilterControl method of the AssociatedControlConverter, in which you can filter the controls to be listed.
The first solution you might jump to is to subclass the AssociatedControlConverter and override the FilterControl method; however there is a better solution.
/// <summary>
/// A generic TypeConverter enabling designer support for assigning the ID of a control to the property on another control
/// </summary>
/// <typeparam name="MemberType">The type of control we wish to create an association with</typeparam>
public class GenericAssociatedControlConverter<MemberType> : ControlIDConverter where MemberType : System.Web.UI.Control
{
/// <summary>
/// Returns true if the control should be included in the list, false otherwise
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
protected override bool FilterControl(System.Web.UI.Control control)
{
if (control is MemberType)
return true;
else
return false;
}
}
Now you can specify the type of control to be listed. For example the following code will filter the list such that only HyperLinks are displayed in the drop down...
/// <summary>
/// Gets or sets a Hyperlink to the atom feed
/// </summary>
[Category("Behavior"),
DefaultValue(""),
Description("Gets or sets a Hyperlink to the atom feed"),
TypeConverter(typeof(GenericAssociatedControl<HyperLink>))]
public string AtomLink
{
get
{
return _atomLinkID;
}
set
{
_atomLinkID = value;
}
}
Considering the AssociatedControlConverter class was new to the 2.0 .NET Framework release I have to wonder why they didn't code the AssociatedControlConverter as a generic class.