Home All Groups Group Topic Archive Search About
Author
6 Jan 2006 7:00 PM
Jamie Nordmeyer
Hi all.  I have a WebControl that has a collection class of objects who have
a property whose type is Control.  I've written a custom TypeConverter for
it that is supposed to present the ID's of all the sited controls on the
page.  It works, too, until you try to run the project, or until you close
the web form, and reopen it in the designer, at which point, if I look at
the the items in the collectin, the Control property will be blank, even
though my designer class properly rendered the property to html.  Here's my
converter.  Any ideas?  Thanks in advance.

public class RequiredControlListTypeConverter : TypeConverter

{

    public override bool GetStandardValuesSupported(ITypeDescriptorContext
context)

    {

        return true;

    }

    public override bool GetStandardValuesExclusive(ITypeDescriptorContext
context)

    {

        return true;

    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type
destinationType)

    {

        if (destinationType == typeof(string))

            return true;

        return base.CanConvertTo (context, destinationType);

    }

    public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)

    {

        if (destinationType == typeof(string) && value is Control)

            return (value as Control).ID;

        return base.ConvertTo (context, culture, value, destinationType);

    }

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type
sourceType)

    {

        if (sourceType == typeof(string))

            return true;

    return base.CanConvertFrom (context, sourceType);

    }

    public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)

    {

        if (context != null)

        {

            if (value is string)

            {

                IDesignerHost host =
(IDesignerHost)context.GetService(typeof(IDesignerHost));

                IReferenceService svc =
(IReferenceService)host.GetService(typeof(IReferenceService));

                return svc.GetReference((string)value);

            }

            return base.ConvertFrom(context, culture, value);

        } // if (context != null)

        else

            return null;

    }

    public override
System.ComponentModel.TypeConverter.StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)

    {

        IDesignerHost host =
(IDesignerHost)context.GetService(typeof(IDesignerHost));

        Page page = (Page)host.RootComponent;

        StringCollection ctrlIDs = new StringCollection();

        FindControls(page.Controls, ctrlIDs);

        return new StandardValuesCollection(ctrlIDs);

    }

    protected void FindControls(ControlCollection ctrls, StringCollection
ctrlIDs)

    {

        foreach(Control ctrl in ctrls)

        {

            if (ctrl.HasControls())

                FindControls(ctrl.Controls, ctrlIDs);

            if
(TypeDescriptor.GetAttributes(ctrl)[typeof(ValidationPropertyAttribute)] !=
null)

                ctrlIDs.Add(ctrl.UniqueID);

        }

    }

}

Author
7 Jan 2006 8:29 AM
Teemu Keiski
Hi,

can you also show how you have implemented the property?

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

Show quoteHide quote
"Jamie Nordmeyer" <jamie.nordme***@mben.com> wrote in message
news:%23vEM5MvEGHA.916@TK2MSFTNGP10.phx.gbl...
> Hi all.  I have a WebControl that has a collection class of objects who
> have a property whose type is Control.  I've written a custom
> TypeConverter for it that is supposed to present the ID's of all the sited
> controls on the page.  It works, too, until you try to run the project, or
> until you close the web form, and reopen it in the designer, at which
> point, if I look at the the items in the collectin, the Control property
> will be blank, even though my designer class properly rendered the
> property to html.  Here's my converter.  Any ideas?  Thanks in advance.
>
> public class RequiredControlListTypeConverter : TypeConverter
>
> {
>
>    public override bool GetStandardValuesSupported(ITypeDescriptorContext
> context)
>
>    {
>
>        return true;
>
>    }
>
>    public override bool GetStandardValuesExclusive(ITypeDescriptorContext
> context)
>
>    {
>
>        return true;
>
>    }
>
>    public override bool CanConvertTo(ITypeDescriptorContext context, Type
> destinationType)
>
>    {
>
>        if (destinationType == typeof(string))
>
>            return true;
>
>        return base.CanConvertTo (context, destinationType);
>
>    }
>
>    public override object ConvertTo(ITypeDescriptorContext context,
> System.Globalization.CultureInfo culture, object value, Type
> destinationType)
>
>    {
>
>        if (destinationType == typeof(string) && value is Control)
>
>            return (value as Control).ID;
>
>        return base.ConvertTo (context, culture, value, destinationType);
>
>    }
>
>    public override bool CanConvertFrom(ITypeDescriptorContext context,
> Type sourceType)
>
>    {
>
>        if (sourceType == typeof(string))
>
>            return true;
>
>    return base.CanConvertFrom (context, sourceType);
>
>    }
>
>    public override object ConvertFrom(ITypeDescriptorContext context,
> System.Globalization.CultureInfo culture, object value)
>
>    {
>
>        if (context != null)
>
>        {
>
>            if (value is string)
>
>            {
>
>                IDesignerHost host =
> (IDesignerHost)context.GetService(typeof(IDesignerHost));
>
>                IReferenceService svc =
> (IReferenceService)host.GetService(typeof(IReferenceService));
>
>                return svc.GetReference((string)value);
>
>            }
>
>            return base.ConvertFrom(context, culture, value);
>
>        } // if (context != null)
>
>        else
>
>            return null;
>
>    }
>
>    public override
> System.ComponentModel.TypeConverter.StandardValuesCollection
> GetStandardValues(ITypeDescriptorContext context)
>
>    {
>
>        IDesignerHost host =
> (IDesignerHost)context.GetService(typeof(IDesignerHost));
>
>        Page page = (Page)host.RootComponent;
>
>        StringCollection ctrlIDs = new StringCollection();
>
>        FindControls(page.Controls, ctrlIDs);
>
>        return new StandardValuesCollection(ctrlIDs);
>
>    }
>
>    protected void FindControls(ControlCollection ctrls, StringCollection
> ctrlIDs)
>
>    {
>
>        foreach(Control ctrl in ctrls)
>
>        {
>
>            if (ctrl.HasControls())
>
>                FindControls(ctrl.Controls, ctrlIDs);
>
>            if
> (TypeDescriptor.GetAttributes(ctrl)[typeof(ValidationPropertyAttribute)]
> != null)
>
>                ctrlIDs.Add(ctrl.UniqueID);
>
>        }
>
>    }
>
> }
>
>
Author
9 Jan 2006 4:56 PM
Jamie Nordmeyer
Sure.  Here ya be:

[Description("Sets or gets the control that will be validated.")]

[Category("Condition"), DefaultValue(null)]

[TypeConverter(typeof(RequiredControlListTypeConverter))]

public Control ControlToValidate

{

    get {return m_ControlToValidate;}

    set

    {

        if (m_ControlToValidate != value)

        {

            m_ControlToValidate = value;

            OnControlToValidateChanged(EventArgs.Empty);

        }

    }

}

Show quoteHide quote
"Teemu Keiski" <jot***@aspalliance.com> wrote in message
news:ugflAS2EGHA.3576@TK2MSFTNGP10.phx.gbl...
> Hi,
>
> can you also show how you have implemented the property?
>
> --
> Teemu Keiski
> ASP.NET MVP, AspInsider
> Finland, EU
> http://blogs.aspadvice.com/joteke
>
> "Jamie Nordmeyer" <jamie.nordme***@mben.com> wrote in message
> news:%23vEM5MvEGHA.916@TK2MSFTNGP10.phx.gbl...
>> Hi all.  I have a WebControl that has a collection class of objects who
>> have a property whose type is Control.  I've written a custom
>> TypeConverter for it that is supposed to present the ID's of all the
>> sited controls on the page.  It works, too, until you try to run the
>> project, or until you close the web form, and reopen it in the designer,
>> at which point, if I look at the the items in the collectin, the Control
>> property will be blank, even though my designer class properly rendered
>> the property to html.  Here's my converter.  Any ideas?  Thanks in
>> advance.
>>
>> public class RequiredControlListTypeConverter : TypeConverter
>>
>> {
>>
>>    public override bool GetStandardValuesSupported(ITypeDescriptorContext
>> context)
>>
>>    {
>>
>>        return true;
>>
>>    }
>>
>>    public override bool GetStandardValuesExclusive(ITypeDescriptorContext
>> context)
>>
>>    {
>>
>>        return true;
>>
>>    }
>>
>>    public override bool CanConvertTo(ITypeDescriptorContext context, Type
>> destinationType)
>>
>>    {
>>
>>        if (destinationType == typeof(string))
>>
>>            return true;
>>
>>        return base.CanConvertTo (context, destinationType);
>>
>>    }
>>
>>    public override object ConvertTo(ITypeDescriptorContext context,
>> System.Globalization.CultureInfo culture, object value, Type
>> destinationType)
>>
>>    {
>>
>>        if (destinationType == typeof(string) && value is Control)
>>
>>            return (value as Control).ID;
>>
>>        return base.ConvertTo (context, culture, value, destinationType);
>>
>>    }
>>
>>    public override bool CanConvertFrom(ITypeDescriptorContext context,
>> Type sourceType)
>>
>>    {
>>
>>        if (sourceType == typeof(string))
>>
>>            return true;
>>
>>    return base.CanConvertFrom (context, sourceType);
>>
>>    }
>>
>>    public override object ConvertFrom(ITypeDescriptorContext context,
>> System.Globalization.CultureInfo culture, object value)
>>
>>    {
>>
>>        if (context != null)
>>
>>        {
>>
>>            if (value is string)
>>
>>            {
>>
>>                IDesignerHost host =
>> (IDesignerHost)context.GetService(typeof(IDesignerHost));
>>
>>                IReferenceService svc =
>> (IReferenceService)host.GetService(typeof(IReferenceService));
>>
>>                return svc.GetReference((string)value);
>>
>>            }
>>
>>            return base.ConvertFrom(context, culture, value);
>>
>>        } // if (context != null)
>>
>>        else
>>
>>            return null;
>>
>>    }
>>
>>    public override
>> System.ComponentModel.TypeConverter.StandardValuesCollection
>> GetStandardValues(ITypeDescriptorContext context)
>>
>>    {
>>
>>        IDesignerHost host =
>> (IDesignerHost)context.GetService(typeof(IDesignerHost));
>>
>>        Page page = (Page)host.RootComponent;
>>
>>        StringCollection ctrlIDs = new StringCollection();
>>
>>        FindControls(page.Controls, ctrlIDs);
>>
>>        return new StandardValuesCollection(ctrlIDs);
>>
>>    }
>>
>>    protected void FindControls(ControlCollection ctrls, StringCollection
>> ctrlIDs)
>>
>>    {
>>
>>        foreach(Control ctrl in ctrls)
>>
>>        {
>>
>>            if (ctrl.HasControls())
>>
>>                FindControls(ctrl.Controls, ctrlIDs);
>>
>>            if
>> (TypeDescriptor.GetAttributes(ctrl)[typeof(ValidationPropertyAttribute)]
>> != null)
>>
>>                ctrlIDs.Add(ctrl.UniqueID);
>>
>>        }
>>
>>    }
>>
>> }
>>
>>
>
>