Developer forum

Forum » Development » Default value for itemfield

Default value for itemfield

Lars Larsen
Reply

Hi,

Using the Code First approch I have made an item with an itemfield of type DateTime. The field should have todays date as the default value. Therefore I have added this line of code "[DefaultValue("@Code(System.DateTime.Now)")]". But still new items based on the itemtype does not get todays date as the default date in the field ("Tidspunkt"). Here is my code:

        [Field("Tidspunkt", typeof(DateTimeEditor))]
        [Group("E-mail notifikation")]
        [DefaultValue("@Code(System.DateTime.Now)")]
        public DateTime NotificationTime { get; set; }

Why does this code not work? Any ideas?


Replies

 
Morten Fink Eriksen
Reply

Hi Lars

Have you tried doing the following?

private DateTime? notificationTime;

        [Field("Tidspunkt", typeof(DateTimeEditor))]
        [Group("E-mail notifikation")]
        public DateTime NotificationTime
        {
            get
            {
                if (this.notificationTime.HasValue)
                    return this.notificationTime.Value;
                else
                    return System.DateTime.Now;
            }
            set
            {
                this.notificationTime = value;
            }
        }

Best Regards 

Morten Fink Buchhave

 
Lars Larsen
Reply

Hi Morten

Thanks for your suggestion. While I was trying to test your code in my solution I discovered a javascript error on the page-properties page. I think this can be why I have problems showing the default date in the field. I have reported the error to DW Support and awaits their answer.

 
Lars Larsen
Reply

Hi

found out that from v8.3.0.0 it's possible to set todays date as the default date by using the DefaultValue attributte like this:

[DefaultValue("Now")]

The string "NOW" used as a default date value is documented here: http://manual.dynamicweb-cms.com/Dynamicweb-On-line-Manual/Items/Item-fields.aspx

 

You must be logged in to post in the forum