Home > DeveloperSection > Forums > timer control not working in ascx page
Monaj Singh
Monaj Singh

Total Post:41

Points:287
Posted on    April-02-2015 8:07 AM

 ASP.Net ASP.Net 
Ratings:


 1 Reply(s)
 640  View(s)
Rate this:

it works in aspx page but does not fire in ascx control. here is the code of ascx. Please let me know what I am missing. All the remaining controls fire but the timer does not fire in my aspx page:

 

    <asp:ScriptManager ID= "SM1" runat="server" EnablePartialRendering="true">

</asp:ScriptManager>

<asp:UpdatePanel id="updPnl" runat="server" UpdateMode="Conditional"  ChildrenAsTriggers="TRUE">

<Triggers>

<asp:AsyncPostBackTrigger ControlID="timer1" EventName="Tick"  />

 

</Triggers>

<ContentTemplate>

  <asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="timer1_tick"></asp:Timer>

    <asp:Label ID="lblTimer" runat="server" Interval="1000" Enabled="False"></asp:Label>

 

 protected void Page_Load(object sender, EventArgs e)

        {

 if (!SM1.IsInAsyncPostBack)

                Session["timeout"] = DateTime.Now.AddMinutes(5).ToString();

            if (!IsPostBack)

            {

                //ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "javascript:f1();", true);

                fillQuestionsLabel();

 

                timer1.Enabled = true;

            }

 

        }

        protected void timer1_tick(object sender, EventArgs e)

        {

            if (0 > DateTime.Compare(DateTime.Now,

           DateTime.Parse(Session["timeout"].ToString())))

            {

                lblTimer.Text = "Number of Minutes Left: " +

                ((Int32)DateTime.Parse(Session["timeout"].

                ToString()).Subtract(DateTime.Now).TotalMinutes).ToString();

            }

        }



Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    April-02-2015 8:14 AM

Hi Monaj,

The timer references Timer1 but when the user control gets rendered it will have a newly assigned name because it is on a user control. Do a View Source and you will see this is the case

<asp:AsyncPostBackTrigger ControlID="timer1" EventName="Tick"  />

Add ClientIDMode="Static" as shown below. This assumes this control is only dropped on the page one time, otherwise you should set ControlID of timer1 postback triggers to Timer1.ClientID()

 

<asp:Timer ID="timer1"
ClientIDMode="Static" runat="server"
Interval="1000" OnTick="timer1_tick"></asp:Timer>


Don't want to miss updates? Please click the below button!

Follow MindStick