0

私は自分の ASPX ページにタイマーを配置しました。ティックで、Label1 という名前の asp:label の "currenttime" を更新しますが、ページが読み込まれると、"endtime" という DateTime 値を保存します。 "currenttime" equals "endtime" fire Response.Redirect(...), 私のコードは以下のようです,

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>

コードビハインド

protected void Timer1_Tick(object sender, EventArgs e)
    {
        if(...comparison goes here....){

        Response.Redirect("Nextpage.aspx")
        }else{
        Label1.Text = DateTime.Now.ToLongTimeString();
        }
    }

解決策を教えてください

4

2 に答える 2

3

TimeOfDayプロパティを使用するだけです:

if (currentTime.TimeOfDay == endTime.TimeOfDay)

>=...の代わりに使用したいと思いますが==。もしそうなら、真夜中のendTime直前の状況に注意してください... 真夜中のcurrentTime直前なっても、まだ発砲したいかもしれません.

正直なところ、完全な日付/時刻を比較するだけでなく、時刻だけを比較したい理由がよくわかりません。

于 2013-09-21T07:18:17.680 に答える
0

方法が使えるかもしれませんToString()http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htmこのアドレスを使用して日付値を再配置し、

if (currentTime.ToString("yyyyMMdd")== endTime.ToString("yyyyMMdd")
于 2013-09-21T08:00:37.680 に答える