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