Dynamic Controls And The UpdateProgress Nightmare

It’s one of the most common issues on the ASP.Net Web 2.0 era, how to add dynamic controls at server side… but that’s a well explored territory as you can check by simply googling it. As a reference I recommend this series of 4 articles.

There are 2 problems not so easy to handle, the first one I can’t remember my source but the point is that when you want to add new controls based on an Ajax call you’re faced with a dilemma. You can only add controls until the OnInit event (after that the viewstate is read and the viewstatetree created/locked meaning any new controls you add would not be persisted to the viewstate, even if correctly recreated). But if you’re doing an Ajax call originating from an ASP.Net control then how do you do your magic?

The answer to that question is that you need to consult Page.Request.Params[“__EVENTTARGET”] and do an if/switch for the control you’re expecting the special trigger. But there’s a catch… pure HTML buttons (or the normal ASP.Net Button which renders a pure HTML button) don’t ever appear on that parameter. This has to do with the way browsers handle submits, but here’s an handy function you can invoke from your PreInit to get the correct control:

public static Control GetPostBackControl(Page page)
Control control = null;
string controlName = page.Request.Params["__EVENTTARGET"];
if (String.IsNullOrEmpty(controlName))
foreach (string item in page.Request.Form)
if (page.FindControl(item) is System.Web.UI.WebControls.Button)
return page.FindControl(item);
return page.FindControl(controlName);

This works because while the __EVENTTARGET param never gets data from a button, the clicked button (and here’s the trick!) is the ONLY button getting listed on the forms collection. Depending on your code you might need to tune the if a little bit if you’re expecting some other kind of control that renders pure HTML buttons.

Another piece of gold, is that while you can easily and quickly find a way with those techniques to add n dynamic UpdatePanel, or dynamic contents inside and UpdatePanel, there is one “brother” that’s much more difficult to figure out. Its name is UpdateProgress and while it may sound stupid it doesn’t provide an easy and quick way of adding dynamic contents. In the case of the UpdatePanel you can simply use


But the UpdateProgress.ProgressTemplate doesn’t get initialized automatically like the previous. Here’s what you need to do:

updateprogress.ProgressTemplate = new ProgressTemplate();

And in case you’re wondering where does that ProgressTemplate comes from:

private class ProgressTemplate : ITemplate
#region ITemplate Members

public void InstantiateIn(Control container)
{ }


There’s the trick. How stupid it might seem the amount of code for something that should be like updateProgress.Controls.Add(control) but my guess is that someone was half asleep at the ASP.Net team 😛

Anyway, if you notice the pieces of code for the basic addition of controls on those fancy pretty and easy controls, maybe you share the feeling that sometimes we pay in characters for the sake of nice and pretty designer-time support… which is good, but sometimes it should be plain easier to figure it out for the hard-coders don’t you think?

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: , , , ,

One Response to “Dynamic Controls And The UpdateProgress Nightmare”

  1. dmitry says:

    thx good article

    Regards from

Leave a Reply

For spam filtering purposes, please copy the number 2896 to the field below: