Search code examples
asp.nettimerdatalist

ASP.NET Update datalist by timer is not working


I have an updatepanel with a label and a datalist inside as follows

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">     
  <ContentTemplate>    
      <asp:Timer ID="Timer1" runat="server" Interval="2000" Enabled="True" ontick="Timer1_Tick"></asp:Timer>
      Tick <asp:Label ID="LabelTick" runat="server" Text=""></asp:Label>
      <asp:DataList ID="dl1" runat="server" DataSourceID="SqlDataSource1" RepeatColumns = "4" CellSpacing = "3" RepeatLayout = "Table">
         <ItemTemplate>
            ...
         </ItemTemplate>
      </asp:DataList>
      <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" SelectCommand="SELECT ..."></asp:SqlDataSource> 
   </ContentTemplate>
</asp:UpdatePanel>

In the code-behind I update the databind after every tick:

Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick

    dl1.DataSourceID = SqlDataSource1.ID
    dl1.DataBind()

    LabelTick.Text = DateTime.Now.ToString()

End Sub

The label is updated correctly every 2 seconds, which means the timer runs correctly. But the DataList is not refreshed, it is only refreshed once after page load. Why?


Solution

  • At the end of the tick call:

     UpdatePanel1.Update()