This is a migrated thread and some comments may be shown as answers.

AjaxUpdatedControls not updated on Timer

4 Answers 184 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
TonyG
Top achievements
Rank 1
TonyG asked on 10 Nov 2011, 08:58 AM
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!

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Nov 2011, 12:10 PM
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
0
TonyG
Top achievements
Rank 1
answered on 26 Nov 2011, 05:57 AM
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.
0
TonyG
Top achievements
Rank 1
answered on 05 Dec 2011, 11:17 PM
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.
0
Iana Tsolova
Telerik team
answered on 06 Dec 2011, 05:53 AM
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
Tags
Ajax
Asked by
TonyG
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
TonyG
Top achievements
Rank 1
Iana Tsolova
Telerik team
Share this question
or