I have created a countdown timer with the help of a code on internet by Member 4332221 on Code Project with some updates in it and I am using this timer on a project for online testing
我已经在代码项目上的成员4332221的互联网代码的帮助下创建了一个倒数计时器,其中有一些更新,我在一个项目上使用这个计时器进行在线测试
Following is the code
以下是代码
aspx code
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
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";
}
}
}
This code is working properly if I am running it alone but the problem is that if I apply this in my module where I want to start this on button click event it works only once and while clicking on any other button the timer goes back to initial position.
如果我单独运行此代码是正常工作但问题是,如果我在我的模块中应用它,我想在按钮单击事件上启动它只能工作一次,而在点击任何其他按钮时,计时器返回到初始位置。
I tried to apply Postback = false
but then the timer is not displaying.
我试图应用Postback = false但是然后计时器没有显示。
How can I load this timer on button click and maintain its state on click event of other controls
如何在按钮单击时加载此计时器并在其他控件的单击事件上保持其状态
1 个解决方案
#1
1
just change your Page_Load
event with following .. i just added one condition (Session["timeout"] == null || Convert.ToString(Session["timeout"]).Trim() == "")
只需更改你的Page_Load事件,我只需添加一个条件(Session [“timeout”] == null || Convert.ToString(Session [“timeout”])。Trim()==“”)
this is my aspx code
这是我的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>
following is my .cs code i as you can see i already have button and value persist on click of the button
以下是我的.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");
}
#1
1
just change your Page_Load
event with following .. i just added one condition (Session["timeout"] == null || Convert.ToString(Session["timeout"]).Trim() == "")
只需更改你的Page_Load事件,我只需添加一个条件(Session [“timeout”] == null || Convert.ToString(Session [“timeout”])。Trim()==“”)
this is my aspx code
这是我的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>
following is my .cs code i as you can see i already have button and value persist on click of the button
以下是我的.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");
}