Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > TypeConverter Question

Reply
Thread Tools

TypeConverter Question

 
 
Jamie Nordmeyer
Guest
Posts: n/a
 
      01-06-2006
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(IDesigner Host));

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

return svc.GetReference((string)value);

}

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

} // if (context != null)

else

return null;

}

public override
System.ComponentModel.TypeConverter.StandardValues Collection
GetStandardValues(ITypeDescriptorContext context)

{

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

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

}

}

}


 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      01-07-2006
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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(IDesigner Host));
>
> IReferenceService svc =
> (IReferenceService)host.GetService(typeof(IReferen ceService));
>
> return svc.GetReference((string)value);
>
> }
>
> return base.ConvertFrom(context, culture, value);
>
> } // if (context != null)
>
> else
>
> return null;
>
> }
>
> public override
> System.ComponentModel.TypeConverter.StandardValues Collection
> GetStandardValues(ITypeDescriptorContext context)
>
> {
>
> IDesignerHost host =
> (IDesignerHost)context.GetService(typeof(IDesigner Host));
>
> 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);
>
> }
>
> }
>
> }
>
>



 
Reply With Quote
 
 
 
 
Jamie Nordmeyer
Guest
Posts: n/a
 
      01-09-2006
Sure. Here ya be:

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

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

[TypeConverter(typeof(RequiredControlListTypeConver ter))]

public Control ControlToValidate

{

get {return m_ControlToValidate;}

set

{

if (m_ControlToValidate != value)

{

m_ControlToValidate = value;

OnControlToValidateChanged(EventArgs.Empty);

}

}

}

"Teemu Keiski" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> 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(IDesigner Host));
>>
>> IReferenceService svc =
>> (IReferenceService)host.GetService(typeof(IReferen ceService));
>>
>> return svc.GetReference((string)value);
>>
>> }
>>
>> return base.ConvertFrom(context, culture, value);
>>
>> } // if (context != null)
>>
>> else
>>
>> return null;
>>
>> }
>>
>> public override
>> System.ComponentModel.TypeConverter.StandardValues Collection
>> GetStandardValues(ITypeDescriptorContext context)
>>
>> {
>>
>> IDesignerHost host =
>> (IDesignerHost)context.GetService(typeof(IDesigner Host));
>>
>> 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);
>>
>> }
>>
>> }
>>
>> }
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
must be marked as Serializable or have a TypeConverter other than ReferenceConverter to be put in viewstate. jeffpriz@yahoo.com ASP .Net 2 02-23-2005 10:24 PM
Help! Problems with custom TypeConverter and Persistence... Sky Sigal ASP .Net 1 07-14-2004 01:02 AM
must be marked as Serializable or have a TypeConverter other than ReferenceConverter to be put in viewstate Harshdeep Mehta ASP .Net 1 07-08-2004 10:32 AM
TypeConverter for pre-existing system classes? foldface@yahoo.co.uk ASP .Net 0 05-27-2004 04:15 PM
make Serializable or create custom TypeConverter ? Christian ASP .Net 1 07-18-2003 12:42 PM



Advertisments