TypeConverter Question

  • Thread starter Jamie Nordmeyer
  • Start date
J

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

}

}

}
 
T

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

Jamie Nordmeyer said:
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);

}

}

}
 
J

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

}

}

}

Teemu Keiski said:
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 said:
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);

}

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top