RadDatePicker losing format on Postback

2 posts, 1 answers
  1. Chance
    Chance avatar
    4 posts
    Member since:
    Apr 2015

    Posted 08 Jun Link to this post

    I have the format set to be M/d/yyyy, and it works when selecting a date but if there is any postback on the page it changes for format to use the dashes like M-dd-yyyy.  Any ideas how to fix this?

     

     

    <telerik:RadDatePicker ID="radGridDate1" Runat="server" SelectedDate='<%# DateTime.Parse( Eval("Date", "{0:M/dd/yyyy}")) %>' Width="100px" Skin="Bootstrap" >
                                                     <Calendar EnableWeekends="True"  ShowRowHeaders="false" Skin="Bootstrap" > </Calendar>
                                                     <DateInput DateFormat="M/d/yyyy" DisplayDateFormat="M/d/yyyy" LabelWidth="40%">
                                                         <EmptyMessageStyle Resize="None" />
                                                         <ReadOnlyStyle Resize="None" />
                                                         <FocusedStyle Resize="None" />
                                                         <DisabledStyle Resize="None" />
                                                         <InvalidStyle Resize="None" />
                                                         <HoveredStyle Resize="None" />
                                                         <EnabledStyle Resize="None" />
                                                     </DateInput>
                                                     <DatePopupButton HoverImageUrl="" ImageUrl="" />
                                                </telerik:RadDatePicker>

    Thanks
  2. Answer
    Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 09 Jun Link to this post

    Hi Chance,

    The slash(/) is special symbol in the date format string, and it is culture dependent:
    https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx#dateSeparator
    It is used as "Date Separator" and will be changed with "-" can be changed "." according to the current culture.

    So you should escape it using back slash(\). Like "M\/dd\/yyyy"

    See this documentation for further information:
    https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx

    If you are setting the format using C# code you should escape the back slash as well as required in the language syntax.

    So the correct code will be:
    <telerik:RadDatePicker ID="radGridDate1" Runat="server"
    SelectedDate='<%# DateTime.Parse( Eval("Date", "{0:M\\/dd\\/yyyy}")) %>' >
    <DateInput DateFormat="M\/d\/yyyy">
    </DateInput>
    </telerik:RadDatePicker>

    Regards,
    Vasil
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top