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

[Solved] Create with empty textbox

5 Answers 293 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Luis Velez
Top achievements
Rank 1
Luis Velez asked on 27 Apr 2010, 01:39 PM
Is there a way to show the textbox empty, instead of the default date, if the Date property for .Value() is null?

This is what we have:

<%= Html.Telerik().DatePicker() 
             .Name("BusinessTaxReceipt.LicenseExpirationDate") 
             .Value(Model.BusinessTaxReceipt.LicenseExpirationDate)%> 


but would like to do something like this:

 
<%= Html.Telerik().DatePicker() 
            .Name("BusinessTaxReceipt.LicenseExpirationDate") 
            .Value(Model.BusinessTaxReceipt.LicenseExpirationDate ?? string.Empty)%> 


Thanks

5 Answers, 1 is accepted

Sort by
0
Accepted
Georgi Krustev
Telerik team
answered on 27 Apr 2010, 02:25 PM
Hello Luis,

The currently the used code in the excerpt will not work. One possible solution is to set new Nullable<DateTime>() instead of string.Empty. Here is a code snippet showing how to achieve this:
<%= Html.Telerik().DatePicker()
             .Name("BusinessTaxReceipt.LicenseExpirationDate")
             .Value(Model.BusinessTaxReceipt.LicenseExpirationDate ?? new Nullable<DateTime>())%>

If you need to set string.Empty if the date is null you need to patch the Value method into DatePickerBuilder.cs file of the Telerik.Web.Mvc project. Here what you need to chage:

[old]
public DatePickerBuilder Value(string date)
{
    Guard.IsNotNullOrEmpty(date, "date");
 
    DateTime parsedDate;
 
    if (DateTime.TryParse(date, out parsedDate))
    {
        Component.Value = parsedDate;
    }
    else
    {
        throw new ArgumentException(TextResource.StringNotCorrectDate);
    }
    return this;
}

[new]
public DatePickerBuilder Value(string date)
{
    DateTime parsedDate;
 
    if (DateTime.TryParse(date, out parsedDate))
    {
        Component.Value = parsedDate;
    }
    else
    {
        Component.Value = null;
    }
    return this;
}


Best wishes,
Georgi Krustev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Luis Velez
Top achievements
Rank 1
answered on 27 Apr 2010, 03:24 PM
Excellent. Thanks Georgi.
0
Sam Critchley
Top achievements
Rank 1
answered on 26 Aug 2010, 03:17 AM
This didn't work for me

<%= Html.Telerik().DatePicker().Name(ViewData.TemplateInfo.GetFullHtmlFieldName("")).Value(new DateTime?()) %>

I still get: 1/01/0001

Being output. As soon I click it it changes to: 1/01/2001

Whole output is:

<div class="t-widget t-datepicker" id="AddModel_Date"><input class="t-input" id="AddModel_Date-input" name="AddModel.Date" title="AddModel.Date" value="1/01/0001" /><a class="t-link t-icon t-icon-calendar" href="#" tabindex="-1" title="Open the calendar">select date</a></div>



0
Sam Critchley
Top achievements
Rank 1
answered on 26 Aug 2010, 05:36 AM
I think it's because of this: *from DataPickerHtmlBuilder.InputTag()

object valueFromViewData = viewData.Eval(DatePicker.Name);

            if (valueFromViewData != null)
            {
                date = Convert.ToDateTime(valueFromViewData);
            }

My model class isn't DateTime? it's DateTime. So it's always getting DateTime.MinValue (the default date) back here. Guess the fix is to just change my AddModel.
0
Georgi Krustev
Telerik team
answered on 26 Aug 2010, 08:51 AM
Hi Sam Critchley,

The aforementioned behavior is expected depending on the current implementation. I will suggest you set the Value of the datepicker UI compoent in the Template to null, if it cannot be convert.

With the next official release datepicker will set null to the parsed value if it is DateTime.MinValue, which is not valid date for JavaScript Date object.

Regards,
Georgi Krustev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Date/Time Pickers
Asked by
Luis Velez
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Luis Velez
Top achievements
Rank 1
Sam Critchley
Top achievements
Rank 1
Share this question
or