TypeConverter Question

Discussion in 'ASP .Net Web Controls' started by Jamie Nordmeyer, Jan 6, 2006.

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

    }

    }

    }
     
    Jamie Nordmeyer, Jan 6, 2006
    #1
    1. Advertising

  2. Jamie Nordmeyer

    Teemu Keiski Guest

    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" <> wrote in message
    news:%...
    > 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);
    >
    > }
    >
    > }
    >
    > }
    >
    >
     
    Teemu Keiski, Jan 7, 2006
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:%...
    >> 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);
    >>
    >> }
    >>
    >> }
    >>
    >> }
    >>
    >>

    >
    >
     
    Jamie Nordmeyer, Jan 9, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian
    Replies:
    1
    Views:
    479
    Gary Varga
    Jul 18, 2003
  2. Replies:
    0
    Views:
    435
  3. Harshdeep Mehta
    Replies:
    1
    Views:
    2,108
    Teemu Keiski
    Jul 8, 2004
  4. Sky Sigal
    Replies:
    1
    Views:
    503
    Sky Sigal
    Jul 14, 2004
  5. Replies:
    2
    Views:
    1,760
Loading...

Share This Page