Search code examples
asp.netjavascriptasp.net-ajaxtimerascx

How can I have an ascx postback and refresh itself automatically every X seconds?


I have an ascx control bound to a datasource with frequently changing data. Is there a quick way to have an ascx control postback, rebind and refresh itself every X seconds. The ascx control is in an update panel.


Solution

  • Use a timer control from the AJAX toolkit since you are already using it:

    <asp:Timer ID="tmrPolling" runat="server" Interval="10000" ontick="tmrPolling_Tick"></asp:Timer>
    

    Add a trigger to your update panel like:

    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="tmrPolling" EventName="Tick" />
    </Triggers>
    

    Then just implement the tmrPolling_Tick handler:

    protected void tmrPolling_Tick(object sender, EventArgs e)
    {
        // Change your update panel controls and data here.
    }
    

    Do not add the timer within your update panel content area.