C# asp.net中基于ajax的倒计时状态维护
我已经创建了一个倒计时计时器的帮助下,代码项目的成员4332221在互联网上的代码与一些更新,我在一个项目上使用这个计时器进行在线测试 下面是代码 aspx代码C# asp.net中基于ajax的倒计时状态维护,c#,asp.net,ajax,timer,C#,Asp.net,Ajax,Timer,我已经创建了一个倒计时计时器的帮助下,代码项目的成员4332221在互联网上的代码与一些更新,我在一个项目上使用这个计时器进行在线测试 下面是代码 aspx代码 <div> <asp:ScriptManager ID= "SM1" runat="server"></asp:ScriptManager> <asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="timer1_tick"&g
<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>
如果我单独运行该代码,则该代码工作正常,但问题是,如果我在我的模块中应用该代码,我希望在按钮单击事件中启动该代码,则该代码仅工作一次,并且在单击任何其他按钮时,计时器返回初始位置
我试图应用Postback=false
,但是计时器没有显示
如何在按钮单击时加载此计时器并在其他控件的单击事件时保持其状态只需使用以下内容更改您的
页面加载事件。。我刚刚添加了一个条件(Session[“timeout”]==null | | Convert.ToString(Session[“timeout”])。Trim()==”)
这是我的aspx代码
只需使用以下内容更改您的页面加载事件即可。。我刚刚添加了一个条件(Session[“timeout”]==null | | Convert.ToString(Session[“timeout”])。Trim()==”)
这是我的aspx代码
谢谢你!但它不起作用,我正试图触发这个按钮点击事件,这个事件早些时候应用于页面\加载意味着你不想在页面\加载上使用它否!我希望它在按钮点击上,也希望在任何其他按钮点击上保持其状态我无法理解您的要求。。很抱歉但是我把我的测试项目代码。。这可能对你有帮助,汉克斯!我希望它会有帮助,你会把它放在哪里谢谢达瓦尔!但它不起作用,我正试图触发这个按钮点击事件,这个事件早些时候应用于页面\加载意味着你不想在页面\加载上使用它否!我希望它在按钮点击上,也希望在任何其他按钮点击上保持其状态我无法理解您的要求。。很抱歉但是我把我的测试项目代码。。这可能对你有帮助,汉克斯!我希望这会有帮助,你会把它放在哪里
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";
}
}
}
<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>
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");
}