假设我有一个基本的 asp.net C# Web 应用程序,我可以在其中输入不同事件的日期,例如换油。我想为不同的车辆跟踪这个。然后我想计算换油天数的差异。
因此,对于最近的换油,日期差异/时间跨度将从今天开始计算。但是对于以前的换油,我希望在记录集中的每个先前记录之间计算 datediff/timespan。我想,我可以创建一个字段来输入天数,但我认为以编程方式应该很容易,不是吗?
现在,在经典的 ASP 中,我会做这样的事情(事实上,这是我在这个小应用程序的先前版本中所做的):
<%set rs=conn.Execute("select vehiclename,changedate from oilchange order by changedate desc")
do until rs.EOF
dim recordchk,record
recordchk = record
date2=date1
date1=rs("changedate")
if record <> rs("vehcilename") then
record = rs("vehiclename")
end if%>
<%
if record <> recordchk then
%>
<tr><td colspan="9"><hr color="black"/></td></tr>
<tr><td colspan="9"><b><%=rs("vehiclename") %></b></td></tr>
<tr><td valign="top" width="15%"><%=rs("changedate") %> <i>(<%=datediff("d",rs("changedate"),date()) %> days)</i></td>
</tr>
<%else%>
<tr><td valign="top" width="15%"><%=rs("changedate") %> <i>(<%=datediff("d",rs("changedate"),date2) %> days)</i></td>
</tr>
<%end if%>
<%rs.MoveNext
loop
rs.Close
%>
但是我很难过如何在 ASP.NET 中做
对于不同的车辆,我有一个中继器,然后我在里面嵌套了一个中继器来遍历该车辆的换油记录。浏览换油清单就好了,但我不知道如何计算才能让我换油之间的天数。
所以,这是我的代码隐藏......(你可以看到这不是换油,而是吉他弦的变化。)
protected void guitarrepeateritemdatabound(object sender, RepeaterItemEventArgs e)
{
stringsEntities db = new stringsEntities();
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
Repeater StringChangeRepeater = (Repeater)e.Item.FindControl("StringChangeRepeater");
HiddenField guitarIDfield = (HiddenField)e.Item.FindControl("guitarIDint");
int guitarIDint = Convert.ToInt32(guitarIDfield.Value);
Label changedate = (Label)e.Item.FindControl("changedate");
TimeSpan timeBetweenChanges = (DateTime.Now - Convert.ToDateTime(changedate));
var qrystringchangesview = (from s in db.stringchange_view
where s.guitarid == guitarIDint
orderby s.changedate descending
select new
{
guitarID2 = s.guitarid,
s.guitarname,
s.stringname,
s.changedate,
s.gauge
});
StringChangeRepeater.DataSource = qrystringchangesview;
StringChangeRepeater.DataBind();
然后是我的 aspx 页面:
<asp:Repeater ID="GuitarRepeater" runat="server" OnItemDataBound="guitarrepeateritemdatabound">
<ItemTemplate>
<tr>
<td valign="top">
<asp:HiddenField ID="guitarIDint" runat="server" Value='<%# Eval("guitarID") %>' />
<asp:LinkButton OnClick="clicktheinstrument" ID="guitarname" runat="server" Font-Bold="true" CommandArgument='<%#Eval ("guitarID") %>' Text='<%# Eval("guitarname") %>' />
<asp:Repeater runat="server" ID="StringChangeRepeater">
<ItemTemplate>
<table><tr><td style="width:100px">
<asp:Label ID="changedate" runat="server" Text='<%# Eval("changedate","{0:MM/dd/yyyy}") %>'></asp:Label>
(<asp:Label ID="timeBetweenChangesLabel" runat="server"></asp:Label>)
</td><td style="width:200px">
<asp:Label ID="stringname" runat="server" Text='<%# Eval("stringname") %>'></asp:Label>
</td><td style="width:100px">
<asp:Label ID="gauge" runat="server" Text='<%# Eval("gauge") %>'></asp:Label>
</td></tr></table>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color: #F7F7F7">
<td valign="top">
<asp:HiddenField ID="guitarIDint" runat="server" Value='<%# Eval("guitarID") %>' />
<asp:LinkButton OnClick="clicktheinstrument" ID="guitarname" runat="server" Font-Bold="true" CommandArgument='<%#Eval ("guitarID") %>' Text='<%# Eval("guitarname") %>' />
<asp:Repeater runat="server" ID="StringChangeRepeater">
<ItemTemplate>
<table><tr><td style="width:100px">
<asp:Label ID="changedate" runat="server" Text='<%# Eval("changedate","{0:MM/dd/yyyy}") %>'></asp:Label>
(<asp:Label ID="timeBetweenChangesLabel" runat="server"></asp:Label>)
</td><td style="width:200px">
<asp:Label ID="stringname" runat="server" Text='<%# Eval("stringname") %>'></asp:Label>
</td><td style="width:100px">
<asp:Label ID="gauge" runat="server" Text='<%# Eval("gauge") %>'></asp:Label>
</td></tr></table>
</ItemTemplate>
</asp:Repeater>
</td>
</tr>
</AlternatingItemTemplate>
</asp:Repeater>