kavin
kavin

Reputation: 1

How to avoid timer reset in asp.net/ajax

I am using updatepanel and timer to display an alert every 15 seconds. It is working fine when I am not clicking anything in the page. It displays alert every 15 seconds. I have a button outside of this updatepanel. Whenever I click this button, the timer resets and it doesn't display alert every 15 seconds. If I stops clicking the button, it starts to display the alert after 15 seconds. BAsically, timer resets the interval when ever I click teh button. I want to display the alert regardless clicking a button or not. Please help me.

in ASPX page

     <asp:ScriptManager ID="ScriptManager1" runat="server">

       </asp:ScriptManager>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"      
         ViewStateMode="Enabled">
  <Triggers>
  <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
   </Triggers>
 </asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="15000" OnTick="Timer1_Tick">
</asp:Timer>

In .CS page

public void Timer1_Tick(object sender, EventArgs e) {

       ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(Page), "ToggleScript", "  
          alert('Hello')", true);

    }

Upvotes: 0

Views: 1127

Answers (1)

Seminda
Seminda

Reputation: 1773

Reason looks lik your button do a post back and your timer in out side the update panel so it will reset.

if you can place your button in to another update panel this will work.

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ViewStateMode="Enabled">
    <ContentTemplate>
        <asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="Timer1_Tick">
        </asp:Timer>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    </ContentTemplate>
</asp:UpdatePanel>

Upvotes: 0

Related Questions