我很生气我把一个带有计时器和标签的更新面板:
<asp:UpdatePanel runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false" ID="upTimer">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick" ></asp:Timer>
</ContentTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="Lab1"></asp:Label>
<br/>
<asp:Label runat="server" ID="lbTime"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
并且在codebehind中我写了将DateTime.Now设置为在“PageLoad”中标记它不应该每秒更新但它正在更新:((
代码背后:
public DateTime endTime;
public TimeSpan timespann;
protected void Page_Load(object sender, EventArgs e)
{
endTime = DateTime.Now.AddHours(1);
lbTime.Text = DateTime.Now.ToString();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
//timespann = endTime.Subtract(DateTime.Now);
//DateTime now = DateTime.Now;
//Response.Write(endTime.Subtract(now).TotalSeconds.ToString());
//Lab1.Text = timespann.ToString();
//lbTime.Text = DateTime.Now.ToString();
}
答案 0 :(得分:0)
当Tick
事件触发时,会导致PostBack
事件导致页面重新加载;因此标签每秒更新一次。
要阻止它更新,您需要使用Page_Load
将代码包装在!IsPostBack
事件中,例如
if (!IsPostBack)
{
endTime = DateTime.Now.AddHours(1);
lbTime.Text = DateTime.Now.ToString();
}