AjaxUpdatedControls not updated on Timer

5 posts, 0 answers
  1. TonyG
    TonyG avatar
    239 posts
    Member since:
    Feb 2006

    Posted 10 Nov 2011 Link to this post

    I have a webform with a timer and various other controls.  When a button is pressed, the server Enables the timer.  The timer is obviously activated, as postbacks start with the OnTick event.  But once the timer starts ticking, none of the UI components are updated as defined by the ajax settings:
    <asp:Panel ID="pnlTimer" runat="server">
      <asp:Timer ID="Timer1" runat="server" Interval="2000" OnTick="Timer1_Tick"
        Enabled="false">
      </asp:Timer>
    </asp:Panel>
    ...
    <telerik:AjaxSetting AjaxControlID="Timer1">
      <UpdatedControls>
        <telerik:AjaxUpdatedControl ControlID="pnlTimer" />
        <telerik:AjaxUpdatedControl ControlID="tbah" />
        <telerik:AjaxUpdatedControl ControlID="grPosts" />
        <telerik:AjaxUpdatedControl ControlID="lblErr" />
        <telerik:AjaxUpdatedControl ControlID="lblMsg" />
      </UpdatedControls>
    </telerik:AjaxSetting>

    The intent here is that on each timer postback some activity will be done which may update an error message or an informational message, and some conditions may cause the timer itself to change interval or disable.  But the labels are not updated, and neither is the timer.

    I have a button defined as start/stop, and if I click that the timer does disable.

    Note that I have the timer wrapped in a panel to accommodate the old issue where updating the timer throws an exception because it's trying to set a Visible property on the timer which doesn't exist.

    What am I missing?  I'm sure it's something simple...  Thanks!
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 10 Nov 2011 Link to this post

    Hello,

    Please check below code snippet.

    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                   <telerik:AjaxSetting AjaxControlID="Button1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="Panel1"  />
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="Button2">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="Panel1"/>
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="Panel1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="Panel1" LoadingPanelID="RadAjaxLoadingPanel1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
        </telerik:RadAjaxLoadingPanel>
        <asp:Panel ID="pnlTimer" runat="server">
            <asp:Button ID="Button1" runat="server" Text="Start" OnClick="Button1_Click" />
            <asp:Button ID="Button2" runat="server" Text="End" OnClick="Button2_Click" />
            <asp:Panel ID="Panel1" runat="server" Height="500px" Width="500px">
                <asp:Timer ID="Timer1" runat="server" Interval="5000" Enabled="false" OnTick="Timer1_Tick">
                </asp:Timer>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </asp:Panel>
        </asp:Panel>
    </div>
    protected void Page_Load(object sender, EventArgs e)
       {
     
       }
       protected void Timer1_Tick(object sender, EventArgs e)
       {
           Label1.Text = DateTime.Now.ToString(); ;
       }
       protected void Button1_Click(object sender, EventArgs e)
       {
           Timer1.Enabled = true;
       }
       protected void Button2_Click(object sender, EventArgs e)
       {
           Timer1.Enabled = false;
       }

    please try with above code and compare with your code.

    Thanks,
    Jayesh Goyani
  3. TonyG
    TonyG avatar
    239 posts
    Member since:
    Feb 2006

    Posted 25 Nov 2011 Link to this post

    Jayesh, I have been experimenting with this for a long time. With careful placement of components in panels, and radajaxmanager-ajaxification of the controls, I was able to get my controls updating. The code you provided above actually broke the page to a point where nothing worked.  :(   Sorry bud.

    Thanks for your response anyway.
  4. TonyG
    TonyG avatar
    239 posts
    Member since:
    Feb 2006

    Posted 05 Dec 2011 Link to this post

    I'm posting here for anyone else who happens across this thread...

    The issue may have been over- or under- ajaxification. After a great exchange in another thread, I decided to avoid doing ajax settings for my timer in markup, and do the settings in codebehind. All other ajax is still done in markup. Here is some of the code I'm using. The method below is called from Page_PreRender(). It decides which ajax settings to use based on which tab I have set for multiple pageviews.

    private void SetAjaxForCurrentTab()
    {
        if ( MainTabs.SelectedTab.Index == 0 )
        {
            SetAjaxManagerForListsTab();
        }
        if ( MainTabs.SelectedTab.Index == 1 )
        {
            SetAjaxManagerForPostsTab();
        }
    }
    Here is one of those routines (manually adjusted a little to make sense here)...
    void SetAjaxManagerForPostsTab()
    {
        AjaxUpdatedControlsCollection c = new AjaxUpdatedControlsCollection();
        c.Add(new AjaxUpdatedControl("gridPosts" , ""));
        c.Add(new AjaxUpdatedControl("lblErr" , ""));
        c.Add(new AjaxUpdatedControl("lblMsg" , ""));
        c.Add(new AjaxUpdatedControl("postsToolbarPanel1" , ""));
        c.Add(new AjaxUpdatedControl("timerRadAjaxPanel1" , ""));
        AjaxSetting s = new AjaxSetting("Timer1");
        s.UpdatedControls.AddRange(c);
        RadAjaxManager1.AjaxSettings.Add(s);
    }
    Here we see a single Timer1 updating several other controls. It does not update controls that are not visible.

    The Timer1 is the only control contained in timerRadAjaxPanel1. Without the ajaxpanel this environment didn't seem to work correctly, though less complex code like Jayesh provided does work without a panel.

    On every postback, we come in with no Timer1 items in the RadAjaxManager1, so the AjaxSettings must be refreshed on every postback.

    In order to make sure all of the settings are OK, right after calling SetAjaxForCurrentTab() I display the settings in a temporary log:
    foreach ( AjaxSetting setting in RadAjaxManager1.AjaxSettings )
    {
        Log("Ajax for " + setting.AjaxControlID);   
        foreach ( AjaxUpdatedControl c in setting.UpdatedControls )
            Log("   " + c.ControlID);
    }

    I will continue to experiment with the various techniques discussed in that other thread. For now, this is the approach I'm using to solve this specific problem.

    I hope that helps someone else.
  5. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 05 Dec 2011 Link to this post

    Hello Tonyg,

    Thank you for sharing your experience, here with the community. I am sure it will help other developers having similar issues, or at least give them idea from where to start.

    Kind regards,
    Iana Tsolova
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Back to Top