【问题标题】:Bind Textbox with textmode localdatetime to DateTime-Field in ObjectDataSource not evaluating将带有文本模式 localdatetime 的文本框绑定到 ObjectDataSource 中的 DateTime-Field 不评估
【发布时间】:2015-05-05 16:40:19
【问题描述】:

我目前正在尝试这个:

<asp:GridView ID="GridView1" runat="server" DataSourceID="ReleasesDataSource"
    AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true"
    AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" PageSize="35"
    ShowFooter="True" ShowHeaderWhenEmpty="True">
    <Columns>
        <asp:TemplateField HeaderText="Release timestamp">
            <ItemTemplate>
                <asp:Label ID="preTimeLabel" runat="server" Text='<%# Eval("preTime") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="preTimeTextBox" runat="server" Text='<%# Bind("preTime"") %>' TextMode="DateTimeLocal"></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
............

时间戳显示正确 - 但是当我进入编辑模式时,我从一个充满占位符的文本框开始。因此,ObjectDataSourcepreTime 字段中的当前 DateTime-Value 不会像我对 Bind() of thepreTime`-Field 所期望的那样进行评估。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 您的代码 sn-p 在 Bind("preTime"") 中显示两个连续的双引号 - 这是代码拼写错误还是您是实际来源?
  • 糟糕,抱歉 - 这确实是代码错误 - 不是我的实际源代码。

标签: c# asp.net datetime


【解决方案1】:

解决此问题所需的正确格式是Bind("preTime", "{0:yyyy-MM-dd}"),如果要包含时间,则为Bind("preTime", "{0:yyyy-MM-ddTHH:mm}")

【讨论】:

    【解决方案2】:

    试试

    <asp:TextBox ID="preTimeTextBox" runat="server" Text='<%# Convert.ToDateTime(Bind("preTime")).ToString("d") %>'></asp:TextBox>
    

    另一个解决方案是为网格中的 TextBox 实现 OnDataBinding 事件。

     protected void txtDate_OnDataBinding(object sender, System.EventArgs e)
    {
    TextBox txt = (TextBox)(sender);
    txt.Text = (DateTime)(Bind("YourDateField")).ToString("MM/dd/yyyy");
    }
    

    【讨论】:

    • 你解决了这个问题吗?请添加评论您是如何解决此问题的?如果堆栈溢出成员提供的答案对您有用,请不要忘记投票并接受答案。如果您认为该答案错误或无用,您也可以投反对票。
    猜你喜欢
    • 1970-01-01
    • 2015-05-29
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多