Displaying milliseconds in TemplateField using Eva

2020-04-20 05:39发布

I have a template filed as listed below. I need to display millisecond part of DateTime also.

I have read about the dateValue.ToString("fff") format in http://msdn.microsoft.com/en-us/library/bb882581.aspx How to: Display Milliseconds in Date and Time Values.

What is the best way to display it in Template Field with Eval?

CODE

 <asp:TemplateField HeaderText="Event Time">

    <ItemTemplate>

       <asp:Literal ID="ltlTime" runat="server" Text='<%# Eval("LastChangeTime") %>' ></asp:Literal>
       <asp:HiddenField ID="hdnMilliSeconds" runat="server" Value='<%# ((DateTime)Eval("LastChangeTime")).ToString("fff") %>' />

    </ItemTemplate>
 </asp:TemplateField>

Reference:

  1. Eval/Bind TimeOfDay property without milliseconds?

3条回答
男人必须洒脱
2楼-- · 2020-04-20 06:11

use

select convert(varchar, your_date_field, 121) as LastChangeTime

in your SQL query. and then access "LastChangeTime" in eval

查看更多
干净又极端
3楼-- · 2020-04-20 06:14

Thanks to @kj nap. I figured it out. For the benefit of others I will post it here:

I used following

 '<%# ((DateTime)Eval("LastChangeTime")).ToString("MM/dd/yyyy hh:mm:ss.fff tt") %>' 

CODE

  <asp:TemplateField HeaderText="Application ID">
                        <ItemTemplate>
                            <asp:Literal ID="ltlApplicationID" runat="server" Text='<%# Eval("ApplicationID") %>'></asp:Literal>
                            <asp:HiddenField ID="hdnLastChangeTime" runat="server" Value='<%# ((DateTime)Eval("LastChangeTime")).ToString("MM/dd/yyyy hh:mm:ss.fff tt") %>' />
                        </ItemTemplate>
  </asp:TemplateField>

CODE BEHIND

    protected void Application_RowCommand(Object sender, CommandEventArgs e)
    {
        if (e != null)
        {
            int rowIndex = Convert.ToInt32(e.CommandArgument, CultureInfo.InvariantCulture);
            string applicationID = (((System.Web.UI.WebControls.Literal)grdApplications.Rows[rowIndex].Cells[1].Controls[1]).Text).Trim();
            string lastChangeTimeString = (((System.Web.UI.WebControls.HiddenField)grdApplications.Rows[rowIndex].Cells[1].Controls[3]).Value).Trim();

            DateTime lastChangeTime = Convert.ToDateTime(lastChangeTimeString);

        }
    }
查看更多
放我归山
4楼-- · 2020-04-20 06:22

Try this

<%# Convert.ToDateTime(Eval("LastChangeTime")).ToString("FFF") %>
查看更多
登录 后发表回答