【问题标题】:html5 input type datetime value not showing on ASP.NET webformhtml5 输入类型日期时间值未显示在 ASP.NET 网络表单上
【发布时间】:2013-05-24 10:03:43
【问题描述】:

编辑 我有一个带有 itemtemplate 的 Asp.Net FormView:

 <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>

当表单处于查看模式时,日期正确显示(当然是设置日期时):

但是,当表单在编辑模式下发生变化时,type="datetime-local" 的输入会显示empty,要求用户选择一个新的日期。这是显示的弹出窗口

但是,我可以看到检查生成的 html 源代码,设置正确...

<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;">

所以有两个问题: 首先是在编辑模式下,我看不到实际值是多少; 第二,如果我只想更改日期或时间,我必须重新输入整个日期...

我的问题是:这是 Html5 计划的行为还是我遗漏了什么?

【问题讨论】:

  • 对我来说,不清楚你在问什么。
  • 我首先在 IE 上测试了它,我没有遇到任何问题。然后我在 Chrome 上试了一下,就明白你的意思了。

标签: asp.net html input webforms


【解决方案1】:

好的,所以问题是 HTML 解析器需要按照 ISO 8601 标准格式化的日期。

不需要在后面的代码中使用自定义格式函数,我最终得到了这个声明性标记:

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

【讨论】:

【解决方案2】:

根据W3 specification

= 本地日期和时间

表示本地日期和时间的字符串。以下部分,在 完全按照以下顺序

  • 日期。
  • 文字字符串“T”。
  • 一次。

例子:

1985-04-12T23:20:50.52

1996-12-19T16:39:57

所以,在你的情况下:

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

会起作用的。

[更新]

您可以使用以下aspx 标记:

<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>

在你后面的代码中,定义如下方法:

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;
}

【讨论】:

  • 谢谢,您的解决方案非常适合显示日期......但现在我不知道如何绑定它(更新什么也不做)
猜你喜欢
  • 2022-11-21
  • 2015-11-17
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-12
  • 1970-01-01
相关资源
最近更新 更多