Error Rendering Control

Discussion in 'ASP .Net Building Controls' started by Benton, Apr 17, 2007.

  1. Benton

    Benton Guest

    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
    Benton, Apr 17, 2007
    #1
    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. Sanjay Pais
    Replies:
    0
    Views:
    2,267
    Sanjay Pais
    Aug 8, 2005
  2. Peter Mount
    Replies:
    4
    Views:
    928
    Peter Mount
    Jan 31, 2006
  3. William French

    rendering derived control from composite control

    William French, Dec 2, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    205
    Wayne
    Dec 3, 2003
  4. Sanjay Pais

    Why do I get the error "Error Rendering Control"

    Sanjay Pais, Aug 8, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    176
    Donald Welker
    Aug 9, 2005
  5. PeterW

    Design Time Error: Error Rendering Control

    PeterW, Mar 29, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    340
    Steven Cheng[MSFT]
    Mar 30, 2006
Loading...

Share This Page