DateFieldElement default value
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
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
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
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
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." title="ConfigElementDictionary<string, FieldDefinitionElement> Telerik.Sitefinity.Web.UI.ContentUI.Config.ContentViewSectionElement.Fields ... ">Fields)
ID = "EventStartDate",
DataFieldName = "EventStart",
Title = "EventStart",
" title="FieldDisplayMode? Telerik.Sitefinity.Web.UI.Fields.Config.FieldControlDefinitionElement.DisplayMode ... ">DisplayMode = new FieldDisplayMode<>" title="System.Nullable<FieldDisplayMode>.Nullable<FieldDisplayMode>(FieldDisplayMode);">?(displayMode),
ResourceClassId = typeof(EventsResources).Name,
WrapperTag = HtmlTextWriterTag.Li,
CssClass = "sfFormSeparator"
;
Thanks,
Duneel
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
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!
Ivan,
Any tips for us?
Thanks
- William
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
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
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
Thanks Ivan!
This is what I was also thinking to explicitly set the default date when an emply item is created.
Thanks,
Duneel