Creating an UpdateProgress programmatically
I’d like to point out that this took me a while to figure out and in the end it was DisturbedBuddha over at the ASP.Net forums that gave me the solution. I thought I would share this with you as I know I would never have been able to imagine how to makes this work on my own.
I used a sample program and constructed it in 2 ways. First I created it the “normal” way by placing all the controls in the .ASPX file. Second I created the same program all in the CodeBehind. I needed to do this becuase I planned to wrap some AJAX functionality into a Composite Control.
I will attach the project at the bottom of this post.
The simple program contained a Label that read “init” on first load, a button that ran a javascript function to postback an event argument that would trigger the Label to be updated to “callback” and display a nice Ajaxy loading animation.
Was able to get everything to work great except I couldn’t get the UdateProgress’s ProgressTemplate created. Below I will post the VB and C# code of how to do this.
The code
VB
Partial Class Default9
Inherits System.Web.UI.Page
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
' !!!THIS MUST BE DONE IN THE PRERENDER EVENT!!!
' Create the label.
Dim lab As New Label
lab.ID = "Label1"
lab.Text = "Please wait..."
' Create the UpdateProgress.
Dim uprog As New UpdateProgress
uprog.AssociatedUpdatePanelID = UpdatePanel1.ClientID
uprog.ID = "UpdateProgress1"
' Create the ProgressTemplate based on a class the implements ITemplate.
Dim value As New MyTemplate
uprog.ProgressTemplate = value
' Add the Label to the UpdateProgress and the UpdateProgress to the form.
uprog.Controls.Add(lab)
Form.Controls.Add(uprog)
End Sub
End Class
' THIS GOES IN YOUR PAGES CODE BEHIND ALSO!
Public Class MyTemplate : Implements ITemplate
Public Sub InstantiateIn(ByVal container As Control) Implements Web.UI.ITemplate.InstantiateIn
' This is empty.
End Sub
End Class
C#
partial class Default9 : System.Web.UI.Page
{
protected void Page_PreRender(object sender, System.EventArgs e)
{
// !!!THIS MUST BE DONE IN THE PRERENDER EVENT!!!
// Create the label.
Label lab = new Label();
lab.ID = "Label1";
lab.Text = "Please wait...";
// Create the UpdateProgress.
UpdateProgress uprog = new UpdateProgress();
uprog.AssociatedUpdatePanelID = UpdatePanel1.ClientID;
uprog.ID = "UpdateProgress1";
// Create the ProgressTemplate based on a class the implements ITemplate.
MyTemplate value = new MyTemplate();
uprog.ProgressTemplate = value;
// Add the Label to the UpdateProgress and the UpdateProgress to the form.
uprog.Controls.Add(lab);
Form.Controls.Add(uprog);
}
}
// THIS GOES IN YOUR PAGES CODE BEHIND ALSO!
public class MyTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
// This is empty.
}
}
I hope this helps someone else out. DisturbedBuddha says he is planning to post on his blog explaining the ins and outs of why this appraoch is necessary.
Download
This project was created using Visual Studio 2008 with the .Net Framework 2.0.
RSS ?

3 comments on this post
No matter what I try with this I can’t set the AssociatedUpdatePanelID I continually get an ajax error stating that it can’t find the update panel with given id.
Cliff: I had the same problem. Try setting the AssociatedUpdatePanelID to the ID of the update panel not the ClientID, it worked for me.
This took some googling! As usual, no documentation! Thank you for sharing this – could have gone on banging my head for days….