Reputation: 428
I have a label in gridview where I display the message sent dateTime depending on whether the message was sent today or earlier.If the message was sent today, it should display the time and if it was sent earlier it displays the date (dd-MM-yyyy). SentDateTime is the column which gives out the required dateTime.
I have tried the below :
<asp:Label ID="lblMsgTime" runat="server" Text='<%# Eval("SentDateTime","{0:dd-MM-yyyy}").Equals(DateTime.Now,"{0:dd-MM-yyyy}")? Eval("SentDateTime","{0:HH:mm}"):Eval("SentDateTime","{0:dd-MM-yyyy}") %>' style="color: #FFFFFF"></asp:Label>
but throws the exception
Member 'object.Equals(object, object)' cannot be accessed with an instance reference; qualify it with a type name instead
Upvotes: 0
Views: 1936
Reputation: 56688
The issue is that you forgot to call ToString
on the DateTime.Now
:
.Equals(DateTime.Now.ToString("{0:dd-MM-yyyy}"))
However this all is very complicated. There is no need to compare strings when dates can be compared directly:
<%# (DateTime)Eval("SentDateTime") == DateTime.Now ? ...
And even this looks too much for a markup. Consider moving this logic to code behind function:
protected string GetDateValue(DateTime sentDateTime)
{
if (sentDateTime == DateTime.Now)
{
return sentDateTime.ToString("HH:mm");
}
else
{
return sentDateTime.ToString("dd-MM-yyyy");
}
}
And then call it like this:
<%# GetDateValue((DateTime)Eval("SentDateTime")) %>
Upvotes: 2