I have put a timer on my ASPX page, Where on tick it updates the "currenttime" on a asp:label called Label1, however when the page loads I store a DateTime value called "endtime", now what i want to do is when "currenttime" equals "endtime" fire Response.Redirect(...), my code is like below,
ASPX
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="1000">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label CssClass="captions2" ID="Label1" runat="server" Text="Exam Timer"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
Code Behind
protected void Timer1_Tick(object sender, EventArgs e)
{
if(...comparison goes here....){
Response.Redirect("Nextpage.aspx")
}else{
Label1.Text = DateTime.Now.ToLongTimeString();
}
}
Please suggest me a solution
Just use the TimeOfDay
property:
if (currentTime.TimeOfDay == endTime.TimeOfDay)
... although I suspect you want to use >=
instead of ==
. If you do, be careful of the situation where endTime
is just before midnight... if currentTime
ends up just after midnight, you may well still want to fire.
To be honest, it's not really clear why you only want to compare the time of day - when not just compare the full date/time?
See more on this question at Stackoverflow