Error Rendering Control

B

Benton

Hi there,

I'm creating a custom server control, inheriting from TextBox. It has this
AsDateTime property that returns the textbox contents converted to the
nullable DateTime data type, as follows:


public class TextBoxWeb : System.Web.UI.WebControls.TextBox
{

[Browsable(false),
Bindable(false),
Category("Behavior"),
DefaultValue(""),
Description("Contents converted to \"DateTime\".")]
public DateTime? AsDateTime
{
get
{
DateTime validDate;

if (DateTime.TryParse(this.Text, out validDate))
return validDate;
else
return null;
}
set
{
this.Text = (value == null) ? string.Empty :
value.Value.ToShortDateString();
}
}
}



Well if I drop this control from the toolbox to a Web Form, the control is
good to go and the property works as expected. So far so good.


But if drop this control inside a Web User Control and then drop the Web
User Control onto a Web Form, my server control does not get rendered on the
designer. Instead I get this error:

Error Rendering Control - TextBoxWeb1

An unhandled exception has ocurred.

Cannot create an object of type 'System.Nullable'1[[System.DateTime,
mscorlib, Version=2.0.0.0., Culture=neutral, PublicKeyToken=b77a5...etc]]'
from its string representation '' for the 'AsDateTime' property.

I struggled with this for hours until I decided to try with the AsDateTime
property as not nullable:

public DateTime AsDateTime
{
get
{
DateTime validDate;

if (DateTime.TryParse(this.Text, out validDate))
return validDate;
else
return DateTime.MinValue;
}
set
{
this.Text = (value == null) ? string.Empty :
value.ToShortDateString();
}
}



And it worked fine both in a web form and in a web user control.

So... any ideas on what might be happening here? Why can't I use a nullable
property? Well okay, I can use it if the control goes straight to a web
form, but I got errors if the control goes to a web user controlfirst and
then the user control goes to a web form.


Anyone else thinks this might be a VS 2005 bug?

Best Regards,

-Benton
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top