This is a migrated thread and some comments may be shown as answers.

DateTime control doesn't show correct default value

1 Answer 98 Views
Input
This is a migrated thread and some comments may be shown as answers.
Charles
Top achievements
Rank 1
Charles asked on 08 Jan 2009, 03:46 PM
I am using the RadDateTime control in a Dynamic Data website as a field template.

The database field has a default binding of CURRENT_TIMESTAMP, but when I bring up a new entry, the RadDateTime control is empty. I would like to have the control set up so that it displays as follows:

- If there is no value returned from the DB, display current timestamp
- If there is a value returned from the DB, display that value

aspx: 
<%@ Control Language="C#" CodeFile="RichDateTime_Edit.ascx.cs" Inherits="Text_EditField" %> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<telerik:RadDateTimePicker SelectedDate='<%# FieldValue %>'  
    ID="RadDateTimePicker1" runat="server" AutoPostBack="True"  
    AutoPostBackControl="Both" Culture="English (United States)" Skin="Telerik"
<TimePopupButton ImageUrl="" HoverImageUrl=""></TimePopupButton> 
 
<TimeView CellSpacing="-1"></TimeView> 
 
    <DateInput AutoPostBack="True"
    </DateInput> 
 
<Calendar UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False"  
        ViewSelectorText="x" Skin="Telerik"></Calendar> 
 
<DatePopupButton ImageUrl="" HoverImageUrl=""></DatePopupButton> 
</telerik:RadDateTimePicker> 
 
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" CssClass="droplist" ControlToValidate="RadDateTimePicker1" Display="Dynamic" Enabled="false" /> 
<asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator1" CssClass="droplist" ControlToValidate="RadDateTimePicker1" Display="Dynamic" Enabled="false" /> 
<asp:DynamicValidator runat="server" ID="DynamicValidator1" CssClass="droplist" ControlToValidate="RadDateTimePicker1" Display="Dynamic" /> 

code-behind:
using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Collections.Specialized; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Xml.Linq; 
using System.Web.DynamicData; 
 
public partial class Text_EditField : System.Web.DynamicData.FieldTemplateUserControl { 
    protected void Page_Load(object sender, EventArgs e) { 
 
        RadDateTimePicker1.ToolTip = Column.Description; 
 
        SetUpValidator(RequiredFieldValidator1); 
        SetUpValidator(RegularExpressionValidator1); 
        SetUpValidator(DynamicValidator1); 
    } 
 
    protected override void ExtractValues(IOrderedDictionary dictionary) 
    { 
        dictionary[Column.Name] = ConvertEditedValue(RadDateTimePicker1.SelectedDate.ToString()); 
    } 
 
    public override Control DataControl { 
        get { 
            return RadDateTimePicker1; 
        } 
    } 
 
 

1 Answer, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 12 Jan 2009, 08:43 AM
Hello Charles,

In order to achieve the desired functionality you may try modifying the RadDateTimePicker's binding expression to check if the value is empty and assign the current date. Similar to the following:

<telerik:RadDateTimePicker SelectedDate='<%# (string.IsNullOrEmpty(FieldValueEditString) || ((DateTime)FieldValue) == DateTime.MinValue) ? DateTime.Now : FieldValue %>' 
<!-- other control setting....--> 

Please give it a try and let us know if this helps.

Greetings,
Rosen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Input
Asked by
Charles
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Share this question
or