Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Expandable property runtime error

Reply
Thread Tools

Expandable property runtime error

 
 
Robert Wilson
Guest
Posts: n/a
 
      12-10-2003
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
----------------------------------


 
Reply With Quote
 
 
 
 
Robert Wilson
Guest
Posts: n/a
 
      12-12-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> ----------------------------------
>
>



 
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
How could I change the NavigateUrl property at runtime based on another controls property ? Radu ASP .Net 2 01-25-2007 10:51 AM
Here is an example (start) of a gridview with expandable fields (not rows) Edwin Knoppert ASP .Net 0 11-29-2005 03:28 PM
xml tree is not expandable in Pocket PC 2003 emulator summer00 ASP .Net 0 03-09-2005 09:36 AM
Expandable container for labels and textboxes Rathtap ASP .Net 0 06-23-2004 03:06 PM
Problems of Defining an Expandable Property for a Web Control in the Designer. Laser Lu ASP .Net Web Controls 1 06-04-2004 02:26 AM



Advertisments