计时器控件无法在ascx页面中工作

时间:2015-01-24 10:06:45

标签: asp.net timer ascx

它在aspx页面中工作,但不会在ascx控件中触发。这是ascx的代码。请让我知道我错过了什么。所有剩余的控件都会触发,但计时器不会在我的aspx页面中触发:

    <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 个答案:

答案 0 :(得分:0)

计时器引用Timer1但是当用户控件被渲染时,它将具有新分配的名称,因为它位于用户控件上。做一个查看源,你会看到这种情况。

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

添加ClientIDMode =&#34;静态&#34;如下所示。这假设此控件仅在页面上被删除一次,否则您应该将Timer1的Timer1回发触发器设置为Timer1.ClientID()

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