Displaying milliseconds in TemplateField using Eval

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try this

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

Method 2

use

select convert(varchar, your_date_field, 121) as LastChangeTime

in your SQL query.
and then access “LastChangeTime” in eval

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

        }
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x