Displaying milliseconds in TemplateField using Eva

2020-04-20 06:30发布

问题:

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?

回答1:

Try this

<%# Convert.ToDateTime(Eval("LastChangeTime")).ToString("FFF") %>


回答2:

use

select convert(varchar, your_date_field, 121) as LastChangeTime

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



回答3:

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

        }
    }