+1-888-365-2779
Try Now
More in this section

Forums / Sitefinity SDK / DateFieldElement default value

DateFieldElement default value

12 posts, 1 answered
  1. Duneel
    Duneel avatar
    166 posts
    Registered:
    08 Dec 2010
    02 May 2011
    Link to this post
    Hi,

    I have couple of date fileds defined in ModuleDefinitions.cs file. I want these fileds be defaulted to current system date. How should I do that?

    Thanks,
    Duneel
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    19 Sep 2016
    03 May 2011
    Link to this post
    Hello Duneel,

    You can use DateFieldElement and its Value property


    var eventStart = new DateFieldElement(mainSection.Fields)
                {
                    ID = "StartDate",
                    DataFieldName = "ItemStart",
                    Title = "v",
                    DisplayMode = displayMode,
                    ResourceClassId = typeof(ResFile).Name,
                    WrapperTag = HtmlTextWriterTag.Li,
                    CssClass = "sfFormSeparator",
                    Value = DateTime.UtcNow,
                };

    All the best,
    Ivan Dimitrov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  3. William
    William avatar
    108 posts
    Registered:
    15 Feb 2011
    04 May 2011
    Link to this post
    Ivan,

    I can't get this to work for me in a custom module in 4.1.  I've tried everything, but I'm still seeing a default date of 1/1/1901, which isn't very workable.  Any other advice?

    Duneel, did that work for you?

    - William
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    19 Sep 2016
    04 May 2011
    Link to this post
    Hi William,

    I am not able to replicate this issue locally. I attached a short video for you. If you call DateTime.UtcNow outside of this control ( for instance use the TextFieldControl) do you get the same date time value?

    Regards,
    Ivan Dimitrov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  5. Duneel
    Duneel avatar
    166 posts
    Registered:
    08 Dec 2010
    04 May 2011
    Link to this post
    Hi Ivan & William,

    It didnt work for me either. I tried to set a TextFieldDefinitionElement and that also didnt work. What I'm thinking is may be when the back-end view is opened, the default values gets wiped off with the null values of the databind object.

    Can you give a try in the new products module and see whether that works in there? It works in the Events module though.

    Using .NET Reflector, I checked in the definitions class of Events module and it doesnt have what you are suggesting us to do. Below is the definition for events and you can see it doesn't have Value=DateTome.UtcNow.

     DateFieldElement parent = new DateFieldElement(element9.Fields) {
                ID = "EventStartDate",
                DataFieldName = "EventStart",
                Title = "EventStart",
                DisplayMode = new FieldDisplayMode?(displayMode),
                ResourceClassId = typeof(EventsResources).Name,
                WrapperTag = HtmlTextWriterTag.Li,
                CssClass = "sfFormSeparator"
            };

    Thanks,
    Duneel
  6. William
    William avatar
    108 posts
    Registered:
    15 Feb 2011
    04 May 2011
    Link to this post
    Thanks Duneel and Ivan,

    Yeah, Duneel, that's what I'm experiencing too.  In fact, if you go to advanced settings and take a look at the field definintions for the custom module once you specify a Value in the definitions code, you'll see the date is correctly entered as the Value field. 

    Problem is, once the backend starts up, as you said, that value is gone and replaced with the null value.  That would be fine if typing in the box wasn't so hard to do given the display of the control above it.

    - William

  7. Duneel
    Duneel avatar
    166 posts
    Registered:
    08 Dec 2010
    04 May 2011
    Link to this post
    You are exactly correct William. You can see the default value when you go to advance settings and then to the field definition. What I believe this is something that Sitefinity will have to handle so that it wont wipe-out the default value if exists. But the good thing is it works in Events module. Ivan may be able to tell us whats the exact fix we need to do.

    Cheers,
    Duneel!
  8. William
    William avatar
    108 posts
    Registered:
    15 Feb 2011
    09 May 2011
    Link to this post
    Ivan,

    Any tips for us?

    Thanks

    - William
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    19 Sep 2016
    12 May 2011
    Link to this post
    Hello William,

    It looks like the definition overrides the config settings, as you can see when the value is set in the definition class it does not get overridden.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  10. Duneel
    Duneel avatar
    166 posts
    Registered:
    08 Dec 2010
    18 May 2011
    Link to this post
    Hi Ivan,

    I still couldnt get this to work. Could you please give a try in Products module and let me know how to set a default date?

    Any luck with you William?

    Thanks,
    Duneel
  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    19 Sep 2016
    19 May 2011
    Link to this post
    Hi Duneel,

    Ok I saw what the problem is. It turned out that the Value is not set. In the data provider you - create and edit methods you need to set the value of the field

    sample

    public override Product CreateProduct(Guid id)
            {
                ....

                productitem.Start= DateTime.UtcNow;
                return productitem;
            }


    In the definition you need to declare the field as shown below

    var productitemStart = new DateFieldElement(mainSection.Fields)
                {
                   ID = "productitemStartID",
                    DataFieldName = "Start",
                    Title = "Start",
                    DisplayMode = displayMode,
                    WrapperTag = HtmlTextWriterTag.Li,
                    CssClass = "sfFormSeparator",
                };

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

    Answered
  12. Duneel
    Duneel avatar
    166 posts
    Registered:
    08 Dec 2010
    19 May 2011
    Link to this post
    Thanks Ivan!

    This is what I was also thinking to explicitly set the default date when an emply item is created.

    Thanks,
    Duneel
12 posts, 1 answered