我已经由Code Project上的4332221会员在互联网上的代码帮助下创建了一个倒计时计时器,并且其中进行了一些更新,并且我正在一个项目上使用此计时器进行在线测试
以下是代码
aspx代码
<div>
<asp:ScriptManager ID= "SM1" runat="server"></asp:ScriptManager>
<asp:Timer ID="timer1" runat="server"
Interval="1000" OnTick="timer1_tick"></asp:Timer>
</div>
<div>
<asp:UpdatePanel id="updPnl"
runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTimer" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer1" EventName ="tick" />
</Triggers>
</asp:UpdatePanel>
</div>
aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!SM1.IsInAsyncPostBack)
Session["timeout"] = DateTime.Now.AddMinutes(1).ToString();
}
protected void timer1_tick(object sender, EventArgs e)
{
if (0 > DateTime.Compare(DateTime.Now,
DateTime.Parse(Session["timeout"].ToString())))
{
int hrs = (((Int32)DateTime.Parse(Session["timeout"].
ToString()).Subtract(DateTime.Now).TotalMinutes)) / 60;
int mins = (((Int32)DateTime.Parse(Session["timeout"].
ToString()).Subtract(DateTime.Now).TotalMinutes)) % 60;
int seconds = (((Int32)DateTime.Parse(Session["timeout"].
ToString()).Subtract(DateTime.Now).TotalSeconds))%60;
lblTimer.Text = "Time left is " + hrs.ToString() + " : " + mins.ToString() + " : " + seconds.ToString();
if (hrs == 0 && mins == 0 && seconds == 0)
{
lblTimer.Text = "Test Time Over";
}
}
}
如果我单独运行,此代码可以正常工作,但是问题是,如果我在我想在按钮单击事件上启动该模块的模块中应用此代码,则该代码只能运行一次,而在单击任何其他按钮时,计时器将返回初始状态。位置。
我尝试申请,Postback = false
但计时器未显示。
如何在单击按钮时加载此计时器,并在其他控件的单击事件中保持其状态
只需更改Page_Load
以下事件即可..我刚刚添加了一个条件(Session["timeout"] == null || Convert.ToString(Session["timeout"]).Trim() == "")
这是我的aspx代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</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">
</asp:Timer>
</div>
<div>
<asp:UpdatePanel ID="updPnl" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTimer" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer1" EventName="tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Button ID="btn2" runat="server" Text="Clickkkk" onclick="btn2_Click" />
</div>
</form>
</body>
</html>
以下是我的.cs代码,如您所见,我已经有按钮,并且单击按钮后值仍然存在
protected void Page_Load(object sender, EventArgs e)
{
if (!SM1.IsInAsyncPostBack && (Session["timeout"] == null || Convert.ToString(Session["timeout"]).Trim() == ""))
Session["timeout"] = DateTime.Now.AddMinutes(3).ToString();
}
protected void timer1_tick(object sender, EventArgs e)
{
if (0 > DateTime.Compare(DateTime.Now,
DateTime.Parse(Session["timeout"].ToString())))
{
int hrs = (((Int32)DateTime.Parse(Session["timeout"].
ToString()).Subtract(DateTime.Now).TotalMinutes)) / 60;
int mins = (((Int32)DateTime.Parse(Session["timeout"].
ToString()).Subtract(DateTime.Now).TotalMinutes)) % 60;
int seconds = (((Int32)DateTime.Parse(Session["timeout"].
ToString()).Subtract(DateTime.Now).TotalSeconds)) % 60;
lblTimer.Text = "Time left is " + hrs.ToString() + " : " + mins.ToString() + " : " + seconds.ToString();
if (hrs == 0 && mins == 0 && seconds == 0)
{
lblTimer.Text = "Test Time Over";
}
}
}
protected void btn2_Click(object sender, EventArgs e)
{
Response.Write("asdasdsa");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句