Hi eleveth,
Here i have created sample that full-fill your requirement.
HTML
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="timer" runat="server" Interval="10">
</asp:Timer>
<asp:DataList ID="dlProducts" runat="server" RepeatColumns="4" CellSpacing="3" RepeatLayout="Table"
OnItemDataBound="dlProducts_ItemDataBound">
<ItemTemplate>
<table>
<tr>
<td>
<asp:HiddenField ID="hfId" runat="server" />
<asp:Label ID="lblCatagory" Text='<%# Eval("Catagory")%>' Visible="false" runat="server" />
<asp:Label ID="lblDetails" Text='<%# Eval("Details")%>' runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Image ID="imgImage" ImageUrl='<%# Eval("Image")%>' runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblPrice" Text='<%# Eval("Price")%>' runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblTime" runat="server" Font-Bold="true" />
<asp:Label ID="lblExpiryTime" Text='<%# Eval("ExpiryDateTime")%>' runat="server"
Visible="false" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
// Get these record from database
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Id"), new DataColumn("Catagory"), new DataColumn("Details"),
new DataColumn("Image"), new DataColumn("Price"), new DataColumn("ExpiryDateTime") });
dt.Rows.Add(1, "Camera", "Nicon", "~/Images/Chrysanthemum.jpg", "$ 104.04", "2017-06-02 23:00:34");
dt.Rows.Add(2, "Laptop", "HP 455 14\"", "~/Images/Desert.jpg", "$6.74", "2017-06-02 22:15:34");
dt.Rows.Add(3, "TV", "Samsung 32\" EH4000G", "~/Images/Hydrangeas.jpg", "$ 39.18", "2017-06-02 22:00:14");
dt.Rows.Add(4, "Coffee", "Nescafe Piccolo", "~/Images/Jellyfish.jpg", "$ 18.88", "2017-06-02 21:58:58");
dlProducts.DataSource = dt;
dlProducts.DataBind();
}
public string CalculateTimeDifference(DateTime startDate, DateTime expiryDate)
{
int hours = 0;
int mins = 0;
int secs = 0;
string final = string.Empty;
if (expiryDate > startDate)
{
hours = expiryDate.Subtract(startDate).Hours;
mins = expiryDate.Subtract(startDate).Minutes;
secs = expiryDate.Subtract(startDate).Seconds;
final = string.Format("{0}:{1}:{2}", hours.ToString().PadLeft(2, '0'), mins.ToString().PadLeft(2, '0'), secs.ToString().PadLeft(2, '0'));
}
return final;
}
protected void dlProducts_ItemDataBound(object sender, DataListItemEventArgs e)
{
DateTime expiryDateTime = Convert.ToDateTime((e.Item.FindControl("lblExpiryTime") as Label).Text);
(e.Item.FindControl("lblTime") as Label).Text = CalculateTimeDifference(DateTime.Now, expiryDateTime);
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
' Get these record from database
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Id"), New DataColumn("Catagory"), New DataColumn("Details"), New DataColumn("Image"), New DataColumn("Price"), New DataColumn("ExpiryDateTime")})
dt.Rows.Add(1, "Camera", "Nicon", "~/Images/Chrysanthemum.jpg", "$ 104.04", "2017-06-02 23:00:34")
dt.Rows.Add(2, "Laptop", "HP 455 14""", "~/Images/Desert.jpg", "$6.74", "2017-06-02 22:15:34")
dt.Rows.Add(3, "TV", "Samsung 32"" EH4000G", "~/Images/Hydrangeas.jpg", "$ 39.18", "2017-06-02 22:00:14")
dt.Rows.Add(4, "Coffee", "Nescafe Piccolo", "~/Images/Jellyfish.jpg", "$ 18.88", "2017-06-02 21:58:58")
dlProducts.DataSource = dt
dlProducts.DataBind()
End Sub
Public Function CalculateTimeDifference(startDate As DateTime, expiryDate As DateTime) As String
Dim hours As Integer = 0
Dim mins As Integer = 0
Dim secs As Integer = 0
Dim final As String = String.Empty
If expiryDate > startDate Then
hours = expiryDate.Subtract(startDate).Hours
mins = expiryDate.Subtract(startDate).Minutes
secs = expiryDate.Subtract(startDate).Seconds
final = String.Format("{0}:{1}:{2}", hours.ToString().PadLeft(2, "0"c), mins.ToString().PadLeft(2, "0"c), secs.ToString().PadLeft(2, "0"c))
End If
Return final
End Function
Protected Sub dlProducts_ItemDataBound(sender As Object, e As DataListItemEventArgs)
Dim expiryDateTime As DateTime = Convert.ToDateTime(TryCast(e.Item.FindControl("lblExpiryTime"), Label).Text)
TryCast(e.Item.FindControl("lblTime"), Label).Text = CalculateTimeDifference(DateTime.Now, expiryDateTime)
End Sub
Screenshot
