Operate with values obtained by Eval () in .aspx


I have a problem that I do not know if it has a solution the way I am facing it.

I have a repeater where I binde through <% # Eval ('property')% > properties of an object, which has all of them, DateTime startTime and DateTime EndTime.

What I would like to do is write in the HTML the hours of difference between these 2 properties, for which I thought I would do something like that,

<%TimeSpan diferencia = ((DateTime)Eval("finaliza")) - ((DateTime)Eval("publicada"));%> 
<%= diferencia.TotalHours %>

which does not work for me and that's why I'm here to find a solution that works. As much as possible through this approach I had, and if it is not possible then I will listen to other options.

Thank you very much.

asked by Barri 23.11.2018 в 19:44

2 answers


I solved it, it was simpler than I thought:

Horas: <%# ((TimeSpan)(((DateTime)Eval("finaliza")) - ((DateTime)Eval("publicada")))).TotalHours%>
answered by 23.11.2018 / 20:39

If you have an object you could define a property readonly that exposes that calculated data

public class Class1 { //aqui seria la clase que defines
   //resto propiedade

   public int Horas {
      get{ return this.finaliza.Subtract(this.publicada).TotalHours; }

then in the html you would use

Horas: <%# Eval('Horas')%>

ealuas the property that exposes the class

answered by 23.11.2018 в 21:48