GenericAssociatedControlConverter - A better AssociatedControlConverter

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.

Published: Friday, August 17, 2007 6:20 PM by Michael Lang
Filed under: ASP.NET
Kick it

Comments

RE: GenericAssociatedControlConverter - A better AssociatedControlConverter

Published: Monday, January 07, 2008 5:46 PM by Raf
Nice, but what happens next? How do I get the value of f.e. ClientID based on the AssociatedControlID property of my derived WebControl?

Thanks!

RE: GenericAssociatedControlConverter - A better AssociatedControlConverter

Published: Tuesday, January 08, 2008 12:13 AM by Michael Lang
Hey Raf..

In this example AtomLink would be set the (server side) ID of a HyperLink selected for your custom control's AtomLink property, you can get an instance of the selected Hyperlink (server side) using...

Hyperlink myAtomLink = FindControl(AtomLink);

You could then get the client side ID of the control using...

string myAtomLinksClientID = myAtomLink.ClientID;

Hope that helps
M

Leave a comment

Your Name (required):
Your Url (optional):
Title (required):
Your Comment (required):

MBLM Logic Check - Protection from spiders and scripts

Guarding against malicious attacks on the web is an unfortunate necessity. To prove your request is authentic, provide answer to the question below.

Enter the answer in numerical format e.g. Q: two + two A: 4
What's forty-one minus twenty-two
 

Home

Microsoft.NET services, software and development resources