R
Robert Wilson
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
----------------------------------
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
----------------------------------