Magnus Rahl
Aug 20, 2010
  9403
(0 votes)

Load page elements asynchronously using UpdatePanel and Timer

If you want to load your pages asynchronously element by element facebook-style to speed up the first display of the page your first choice would probably not be UpdatePanel but rather AJAX using jQuery or a similar framework.

Using updatepanels is however something that is almost always available in EPiServer projects without any modifications. So if you want to perform an asyncronous load in a single location using the “OOTB” controls, this is a way to do it:

Markup:

<asp:ScriptManager runat="server />

<asp:UpdatePanel runat="server" UpdateMode="Conditional" RenderMode="Inline">

    <ContentTemplate>
        <asp:Literal runat="server" ID="litData">Loading...</asp:Literal>
        <asp:Timer runat="server" Interval="1" OnTick="GetData" />
    </ContentTemplate>
</asp:UpdatePanel>

 

Codebehind:

protected void GetData(object sender, EventArgs e)
{
    var timer = sender as Timer;
    if (timer != null)
    {
        timer.Enabled = false;
        litData.Text = Loaded;
    }
}

You can use this technique in databound controls as well, that is where it really might prove interesting when loading several elements which each of them load slowly. In that case you can use the sender object (the timer) as an “entry point” to the control hierarchy to get the elements you want to update (i.e. use timer.Parent.FindControl to find siblings of the timer).

Of course you’d like to put some more interesting content in your panel when it loads, I’m just illustrating the principle. Two important things to note though:

  • Be careful to disable the timer at the first call or it will continue loading the same data over and over.
  • Note the properties of the UpdatePanel. By default it loads as a block element and you might not want that, so set it to render inline. The update must also be conditional or else you’ll get very strange behaviour.
Aug 20, 2010

Comments

Lars Flågan
Lars Flågan Sep 21, 2010 10:33 AM

using the update panel for this sort of functionality is not wise. Have a look at jQuery and jTemplates.

Magnus Rahl
Magnus Rahl Sep 21, 2010 10:33 AM

As I stated in the beginning of the post jQuery or similar would be a better choice. This is to be seen as an alternative second-best approach.

Please login to comment.
Latest blogs
Creating an Optimizely CMS Addon - Adding an Editor Interface Gadget

In   Part One   of this series, I covered getting started with creating your own AddOn for Optimizely CMS 12. This covered what I consider to be an...

Mark Stott | Aug 30, 2024

Configure your own Search & Navigation timeouts

The main blog Configure your own Search & Navigation timeouts was posted for years but you need copy the code to your application. We now bring tho...

Manh Nguyen | Aug 30, 2024

Joining the Optimizely MVP Program

Andy Blyth has been honoured as an Optimizely MVP, recognising his contributions to the Optimizely community. Learn how this achievement will enhan...

Andy Blyth | Aug 29, 2024 | Syndicated blog

Welcome 2024 Summer OMVPs

Hello, Optimizely community! We are thrilled to announce and welcome the newest members to the Optimizely Most Valuable Professionals (OMVP) progra...

Patrick Lam | Aug 29, 2024

Create custom folder type in Edit Mode

Content Folders, which are located in assets pane gadget or multichannel content gadget, allow you to add any type of block or folders. But...

Grzegorz Wiecheć | Aug 28, 2024 | Syndicated blog

Creating an Optimizely AddOn - Getting Started

When Optimizely CMS 12 was launched in the summer of 2021, I created my first AddOn for Optimizely CMS and talked about some of the lessons I learn...

Mark Stott | Aug 28, 2024