Wednesday, March 28, 2012

multiple update panels each with a timer

I have a very simple page. It has 1 script manager and 2 update panels. Each update panel has a label and a timer. the timers are set 1 second and each simply updates the label with the current time. The first timer works and displays the time. The second never fires. Any one have any ideas? Here is the page...

<%

@dotnet.itags.org.PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="WebApplication1._Default" %>

<%

@dotnet.itags.org.RegisterAssembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"Namespace="System.Web.UI"TagPrefix="asp" %>

<!

DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<

htmlxmlns="http://www.w3.org/1999/xhtml">

<

headrunat="server"><title>Untitled Page</title>

</

head>

<

body><formid="form1"runat="server"><div><asp:ScriptManagerID="ScriptManager1"runat="server"></asp:ScriptManager>

</div> <asp:UpdatePanelID="UpdatePanelFirst"runat="server"><ContentTemplate><asp:TimerID="TimerFirst"runat="server"Interval="1000"OnTick="TimerFirst_Tick"></asp:Timer><asp:LabelID="LabelFirst"runat="server"></asp:Label></ContentTemplate></asp:UpdatePanel><asp:UpdatePanelID="UpdatePanel1"runat="server"><ContentTemplate><asp:TimerID="Timer1"runat="server"Interval="1000"OnTick="Timer1_Tick"></asp:Timer><asp:LabelID="Label1"runat="server"Text="Label"></asp:Label></ContentTemplate></asp:UpdatePanel></form>

</

body>

</

html>

and the code behind.

using

System;

using

System.Data;

using

System.Configuration;

using

System.Collections;

using

System.Web;

using

System.Web.Security;

using

System.Web.UI;

using

System.Web.UI.WebControls;

using

System.Web.UI.WebControls.WebParts;

using

System.Web.UI.HtmlControls;

namespace

WebApplication1

{

publicpartialclass_Default : System.Web.UI.Page

{

protectedvoid Page_Load(object sender,EventArgs e)

{

}

protected

void Timer1_Tick(object sender,EventArgs e)

{

Label1.Text =

DateTime.Now.ToShortTimeString();

}

protectedvoid TimerFirst_Tick(object sender,EventArgs e)

{

LabelFirst.Text =

DateTime.Now.ToLongTimeString();

}

}

}

I am not sure about your requirement. If you are trying to update both the labels at the same time one timer will do.

But if you want both the Timer ticks to work modify your code as follows:

protected

void Timer1_Tick(object sender,EventArgs e)

{

Timer1.Enabled =

false;

Timer2.Enabled =

true;

Label1.Text = System.

DateTime.Now.ToLongTimeString();

}

protectedvoid Timer2_Tick(object sender,EventArgs e)

{

Timer2.Enabled =

false;

Timer1.Enabled =

true ;

Label2.Text = System.

DateTime.Now.ToLongTimeString();

}

No comments:

Post a Comment