首页 新闻 会员 周边

asp.net写的Timer事件

0
悬赏园豆:10 [已解决问题] 解决于 2016-01-28 14:40

前台写好Timer标签:

<asp:Label ID="Label4" runat="server" Text="时间"></asp:Label>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:Timer ID="t1" runat="server" Interval="5000" OnTick="Timer1_Tick">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>

 

后台写执行的事件:

protected void Timer1_Tick(object sender, EventArgs e)
{
Label4.Text = DateTime.Now.ToString();
Response.Write("<script>alert('" + DateTime.Now.ToString() + "');</script>");
}

 

页面加载后完全没有变化,但是加断点以后还是能往里进的,只是前台一直没动静,显示不出来,求解!

emsui的主页 emsui | 初学一级 | 园豆:77
提问于:2016-01-28 13:39
< >
分享
最佳答案
1

将第一行的lable标签也放在ajax包裹里面,。这样值就会刷新了
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>

<asp:Label ID="Label4" runat="server" Text="时间"></asp:Label>
<asp:Timer ID="t1" runat="server" Interval="5000" OnTick="Timer1_Tick">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>

后台这么写:

protected void Timer1_Tick(object sender, EventArgs e)
{
Label4.Text = DateTime.Now.ToString();
ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "", "alert('" + DateTime.Now.ToString() + "');", true);
}

收获园豆:10
张大大大虾 | 菜鸟二级 |园豆:225 | 2016-01-28 14:08

OK,有效果了,感激不尽!

emsui | 园豆:77 (初学一级) | 2016-01-28 14:39
其他回答(1)
0

先别撸代码了,看看书吧。  加油。

写代码的小2B | 园豆:4371 (老鸟四级) | 2016-01-28 13:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册