timer control not working in ascx page

Total Post:41

Points:287
 1297  View(s)
Ratings:
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();
            }
        }

  1. Post:397

    Points:3117
    Re: timer control not working in ascx page

    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>

      Modified On Apr-09-2018 04:05:56 AM

Answer