这段代码中的问题在哪里 [英] Where is the problem in this code
本文介绍了这段代码中的问题在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用ASP.NET实现倒数计时器。在显示剩余的分钟数之前它工作正常。
Default.aspx页面中的代码是
Hi,
I was trying to implement a countdown timer using ASP.NET. It is working fine until displaying the remaining minutes.
The code in Default.aspx page is
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Task3.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
position: relative;
top: 33px;
left: 314px;
width: 470px;
height: 193px;
}
.auto-style2 {
width: 202px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="SM1" runat="server"></asp:ScriptManager>
<asp:Timer ID="timer1" runat="server"
Interval="1000" OnTick="timer1_tick" Enabled="False">
</asp:Timer>
</div>
<table class="auto-style1">
<tr>
<td class="auto-style2">
<asp:Label ID="Label1" runat="server" Text="Enter Number of Minutes:"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" TextMode="Number" Height="28px" Width="191px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2"></td>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<div>
<asp:UpdatePanel ID="updPnl"
runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTimer" runat="server" Font-Size="X-Large"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer1" EventName="tick" />
</Triggers>
</asp:UpdatePanel>
</div>
</td>
</tr>
<tr>
<td class="auto-style2"></td>
<td>
<asp:Label ID="lblresult" runat="server"></asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>
default.aspx.cs代码中的代码是
The code in default.aspx.cs code is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Task3
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void timer1_tick(object sender, EventArgs e)
{
if (0 > DateTime.Compare(DateTime.Now,
DateTime.Parse(Session["timeout"].ToString())))
{
lblTimer.Text = "Number of Minutes Left: " +
((Int32)DateTime.Parse(Session["timeout"].
ToString()).Subtract(DateTime.Now).TotalMinutes).ToString();
}
else
{
lblresult.Text = "Countdown Complete";
lblresult.ForeColor = System.Drawing.Color.Green;
timer1.Enabled = false;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
timer1.Enabled=true;
if (!SM1.IsInAsyncPostBack)
{
int time = int.Parse(TextBox1.Text)+1;
Session["timeout"] = DateTime.Now.AddMinutes(time).ToString();
}
}
}
}
这里的问题是
1. else循环不起作用。当我放置制动点时,我每次都要进行页面加载。
2.如何显示剩余秒数
谢谢你
The problems here is
1. The else loop is not working. When I placed a braking point I fount it is going to page load each time.
2. How to display the number of seconds remaining
Thank you
推荐答案
请你看看这篇文章
使用ASP.NET计时器控件创建倒计时器和Ajax [ ^ ]
can you please look into this article
Create Count Down Timer using ASP.NET Timer Control and Ajax[^]
这篇关于这段代码中的问题在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文