Expandable property runtime error

Discussion in 'ASP .Net Building Controls' started by Robert Wilson, Dec 10, 2003.

  1. Hi, could somebody please help.

    I am creating a Custom Control for Asp.Net using C#. The code below creates
    an expandable property called Crop. It works fine in Visual Studio, but when
    I use the custom control in WebForm.aspx I get the following server error:

    "System.Web.HttpException: Unable to generate code for a value of type
    'ImageControl.CropParameter'. This error occurred while trying to generate
    the property value for Crop."

    How can I avoid this error during runtime when using the Crop property?
    Thanks!

    The code is as follows:
    ----------------------------------
    using System;
    using System.Web.UI;
    using System.ComponentModel;
    using System.Globalization;
    using System.ComponentModel.Design.Serialization;
    using System.Reflection;

    namespace ImageControl
    {

    public class ImageCreator : System.Web.UI.WebControls.Image
    {
    private CropParameter _crop = new CropParameter();
    public virtual CropParameter Crop
    {
    get { return _crop; }
    set { _crop = value; }
    }
    }

    [TypeConverter(typeof(CropConverter))]
    public class CropParameter
    {
    private float _cropX=0, _cropY=0;

    [PersistenceMode(PersistenceMode.InnerProperty),
    NotifyParentProperty(true)]
    public float CropX
    {
    get { return _cropX; }
    set { _cropX = value; }
    }

    [PersistenceMode(PersistenceMode.InnerProperty),
    NotifyParentProperty(true)]
    public float CropY
    {
    get { return _cropY; }
    set { _cropY = value; }
    }
    }

    internal class CropConverter : ExpandableObjectConverter
    {
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type
    t)
    {
    if (t==typeof(string)) { return true; }
    return base.CanConvertFrom(context, t);
    }

    public override object ConvertFrom(ITypeDescriptorContext context,
    CultureInfo info, object val)
    {
    if (val is string)
    {
    string str = val as string;
    try
    {
    CropParameter crop1 = new CropParameter();
    if (str==null || str==String.Empty) { return crop1; }
    string[] code= str.Split(',');
    if (code.Length>2) { throw new ApplicationException("Crop
    parameter has too many values."); }
    crop1.CropX = Convert.ToSingle(code[0]);
    if (code.Length==2) { crop1.CropY =
    Convert.ToSingle(code[1]); }
    return crop1;
    }
    catch { throw new ArgumentException("Cannot convert '" + str +"'
    to type CropParameter"); }
    }
    return base.ConvertFrom(context, info, val);
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type
    t)
    {
    if (t==typeof(InstanceDescriptor)) { return true; }
    return base.CanConvertTo(context, t);
    }

    public override object ConvertTo(ITypeDescriptorContext context,
    CultureInfo culture, object val, Type t)
    {
    if (t==typeof(string) && val is CropParameter)
    {
    CropParameter crop = val as CropParameter;
    return (crop.CropX.ToString() + "," +
    crop.CropY.ToString());
    }
    if (t==typeof(InstanceDescriptor) && val is CropParameter)
    {
    CropParameter crop = val as CropParameter;
    System.Type[] argTypes = new System.Type[2];
    argTypes[0] = typeof(float);
    argTypes[1] = typeof(float);
    ConstructorInfo constructor =
    crop.GetType().GetConstructor(argTypes);
    object[] arguments;
    arguments = new object[2];
    arguments[0] = crop.CropX;
    arguments[1] = crop.CropY;
    InstanceDescriptor id = new InstanceDescriptor(constructor,
    arguments);
    return new InstanceDescriptor(constructor, arguments);
    }
    return base.ConvertTo(context, culture, val, t);
    }
    }

    } // end namespace
    ----------------------------------
    Robert Wilson, Dec 10, 2003
    #1
    1. Advertising

  2. I have found the solution. I did not add the constructor CropParameter() to
    the class CropParameter.

    public class CropParameter
    {
    public CropParameter() { }
    public CropParameter(float x, float y)
    {
    CropX=x;
    CropY=y;
    }
    // ... the rest of the code here ...
    }

    I hope this will help someone who has a similar problem.

    Robert



    "Robert Wilson" <> wrote in message
    news:...
    > Hi, could somebody please help.
    >
    > I am creating a Custom Control for Asp.Net using C#. The code below

    creates
    > an expandable property called Crop. It works fine in Visual Studio, but

    when
    > I use the custom control in WebForm.aspx I get the following server error:
    >
    > "System.Web.HttpException: Unable to generate code for a value of type
    > 'ImageControl.CropParameter'. This error occurred while trying to generate
    > the property value for Crop."
    >
    > How can I avoid this error during runtime when using the Crop property?
    > Thanks!
    >
    > The code is as follows:
    > ----------------------------------
    > using System;
    > using System.Web.UI;
    > using System.ComponentModel;
    > using System.Globalization;
    > using System.ComponentModel.Design.Serialization;
    > using System.Reflection;
    >
    > namespace ImageControl
    > {
    >
    > public class ImageCreator : System.Web.UI.WebControls.Image
    > {
    > private CropParameter _crop = new CropParameter();
    > public virtual CropParameter Crop
    > {
    > get { return _crop; }
    > set { _crop = value; }
    > }
    > }
    >
    > [TypeConverter(typeof(CropConverter))]
    > public class CropParameter
    > {
    > private float _cropX=0, _cropY=0;
    >
    > [PersistenceMode(PersistenceMode.InnerProperty),
    > NotifyParentProperty(true)]
    > public float CropX
    > {
    > get { return _cropX; }
    > set { _cropX = value; }
    > }
    >
    > [PersistenceMode(PersistenceMode.InnerProperty),
    > NotifyParentProperty(true)]
    > public float CropY
    > {
    > get { return _cropY; }
    > set { _cropY = value; }
    > }
    > }
    >
    > internal class CropConverter : ExpandableObjectConverter
    > {
    > public override bool CanConvertFrom(ITypeDescriptorContext context,

    Type
    > t)
    > {
    > if (t==typeof(string)) { return true; }
    > return base.CanConvertFrom(context, t);
    > }
    >
    > public override object ConvertFrom(ITypeDescriptorContext context,
    > CultureInfo info, object val)
    > {
    > if (val is string)
    > {
    > string str = val as string;
    > try
    > {
    > CropParameter crop1 = new CropParameter();
    > if (str==null || str==String.Empty) { return crop1; }
    > string[] code= str.Split(',');
    > if (code.Length>2) { throw new ApplicationException("Crop
    > parameter has too many values."); }
    > crop1.CropX = Convert.ToSingle(code[0]);
    > if (code.Length==2) { crop1.CropY =
    > Convert.ToSingle(code[1]); }
    > return crop1;
    > }
    > catch { throw new ArgumentException("Cannot convert '" + str

    +"'
    > to type CropParameter"); }
    > }
    > return base.ConvertFrom(context, info, val);
    > }
    >
    > public override bool CanConvertTo(ITypeDescriptorContext context, Type
    > t)
    > {
    > if (t==typeof(InstanceDescriptor)) { return true; }
    > return base.CanConvertTo(context, t);
    > }
    >
    > public override object ConvertTo(ITypeDescriptorContext context,
    > CultureInfo culture, object val, Type t)
    > {
    > if (t==typeof(string) && val is CropParameter)
    > {
    > CropParameter crop = val as CropParameter;
    > return (crop.CropX.ToString() + "," +
    > crop.CropY.ToString());
    > }
    > if (t==typeof(InstanceDescriptor) && val is CropParameter)
    > {
    > CropParameter crop = val as CropParameter;
    > System.Type[] argTypes = new System.Type[2];
    > argTypes[0] = typeof(float);
    > argTypes[1] = typeof(float);
    > ConstructorInfo constructor =
    > crop.GetType().GetConstructor(argTypes);
    > object[] arguments;
    > arguments = new object[2];
    > arguments[0] = crop.CropX;
    > arguments[1] = crop.CropY;
    > InstanceDescriptor id = new

    InstanceDescriptor(constructor,
    > arguments);
    > return new InstanceDescriptor(constructor, arguments);
    > }
    > return base.ConvertTo(context, culture, val, t);
    > }
    > }
    >
    > } // end namespace
    > ----------------------------------
    >
    >
    Robert Wilson, Dec 12, 2003
    #2
    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. Rathtap
    Replies:
    0
    Views:
    401
    Rathtap
    Jun 23, 2004
  2. summer00
    Replies:
    0
    Views:
    304
    summer00
    Mar 9, 2005
  3. Edwin Knoppert
    Replies:
    0
    Views:
    4,362
    Edwin Knoppert
    Nov 29, 2005
  4. Radu
    Replies:
    2
    Views:
    1,314
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
    Jan 25, 2007
  5. Laser Lu
    Replies:
    1
    Views:
    128
    Laser Lu
    Jun 4, 2004
Loading...

Share This Page