3

Edit I have an Asp.Net FormView, with an itemtemplate:

 <asp:TemplateField HeaderText="DateStart" SortExpression="DateStart">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBoxEditStageDetailsDateStart" type="datetime-local" runat="server"
                            Text='<%# Bind("DateStart") %>' CssClass="TextBoxDateTime"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("DateStart") %>'></asp:Label>
                    </ItemTemplate>
                    <ControlStyle Width="160px" />
                </asp:TemplateField>

When the Form is in View Mode, the date is correctly showed (of course when the date is set):

enter image description here

However, when the Form change in Edit Mode, the input with type="datetime-local" is showed but is empty, requiring the user to choose a new date. This is the Popup showed:

enter image description here

However, as I can see inspecting the generated html source, the value is correctly set...

<input name="ctl00$MainContent$StageDetailsView$TextBoxEditStageDetailsDateStart" value="28/05/2013 10:00:00" id="MainContent_StageDetailsView_TextBoxEditStageDetailsDateStart" class="TextBoxDateTime" type="datetime-local" style="width:160px;">

So two problems: first is that in Edit mode, I am not able to see what the actual value is; second if I only want to change the day or hour I have to re-enter the whole date...

My question is: Is this the Html5 planned behaviour or I am missing something?

2
  • For me, it's not clear what you are asking. Commented May 29, 2013 at 8:35
  • I first tested it on IE and I got no issues. Then I tried it on Chrome and understood what you meant. Commented May 29, 2013 at 8:51

2 Answers 2

6

According to the W3 specification:

value = local date and time

A string representing a local date and time. The following parts, in exactly the following order:

  • A date.
  • The literal string "T".
  • A time.

Example:

1985-04-12T23:20:50.52

1996-12-19T16:39:57

So, in your case:

value="2013-05-28T10:00:00"

will work.

[UPDATE]

You may use the following aspx markup:

<asp:TemplateField HeaderText="DateStart" SortExpression="DateStart">
    <EditItemTemplate>
        <asp:TextBox ID="TextBoxEditStageDetailsDateStart" type="datetime-local" runat="server"
            Text='<%# GetFormattedDate(Eval("DateStart")) %>' CssClass="TextBoxDateTime"></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("DateStart") %>'></asp:Label>
    </ItemTemplate>
    <ControlStyle Width="160px" />
</asp:TemplateField>

In your code behind, define the following method:

protected string GetFormattedDate(object dateTimeObject)
{
    DateTime dateTime;
    if (DateTime.TryParse(dateTimeObject.ToString(), out dateTime))
    {
        return String.Format("{0}-{1}-{2}T{3}:{4}:{5}",
            dateTime.Year,
            dateTime.Month.ToString().PadLeft(2, '0'),
            dateTime.Day.ToString().PadLeft(2, '0'),
            dateTime.Hour.ToString().PadLeft(2, '0'),
            dateTime.Minute.ToString().PadLeft(2, '0'),
            dateTime.Second.ToString().PadLeft(2, '0')
            );
    }
    return null;
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, your solution works perfect for showing the date ... but now I don't know how to Bind it (the Update doesn't do nothing)
2

Ok, so the problem is that the HTML parser wants a Date formatted in the ISO 8601 standard.

Without the need to use a Custom format function in code behind, I ended up with this declarative markup:

<asp:TextBox ID="TextBoxEditStageDetailsDateStart" type="datetime-local" runat="server"
Text='<%# Bind("DateStart", "{0:yyyy-MM-ddTHH:mm:ss}") %>' CssClass="TextBoxDateTime"></asp:TextBox>

1 Comment

This is the standard format 's': msdn.microsoft.com/en-us/library/… so you could just use {0:s}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.