Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

Accessing webcontrols inside the ProgressTemplate of an UpdateProgress

2 min read

Apparently it is not possible to access server-side web controls that are within the "ProgressTemplate" of an UpdateProgress control. If you have for instance the following code
<asp:UpdateProgress ID="updateProgressMain" runat="server" DisplayAfter="100">
<ProgressTemplate>
<div id="progressBackgroundFilter">
</div>
<div id="genericProgressMessage">
<asp:Label ID="lblProgressMessage" runat="server" Text="Loading..."></asp:Label>
<br />
<br />
<asp:Image runat="server" ID="imgProgress" alt="Loading" ImageUrl="~/Images/ajax-loader.gif" />
</div>
</ProgressTemplate>
</asp:UpdateProgress>
then accessing the Label control "lblProgressMessage" like
lblProgressMessage.Text = "Some other text";
from your codebehind code will result in a failure. If you take a look at your designer file you'll notice that there is actually no declaration of the created label and also if you create it manually, the designer will remove it or it will not be instantiated with an object at runtime. This behavior is really strange and unfortunately I don't  have any explanation for it at the moment. It seems as if all server-side controls within the ProgressTemplate are simply ignored. So you can also have a situation like
<asp:Label ID="lblProgressMessage" runat="server" Text="Loading..."></asp:Label>
<asp:UpdateProgress ID="updateProgressMain" runat="server" DisplayAfter="100">
<ProgressTemplate>
<div id="progressBackgroundFilter">
</div>
<div id="genericProgressMessage">
<asp:Label ID="lblProgressMessage" runat="server" Text="Loading..."></asp:Label>
<br />
<br />
<asp:Image runat="server" ID="imgProgress" alt="Loading" ImageUrl="~/Images/ajax-loader.gif" />
</div>
</ProgressTemplate>
</asp:UpdateProgress>
which of course should just be illegal since you have two controls with the same ID, but surprisingly nothing happens. The code compiles without any problems. Generally I would suggest to not use any server-side controls inside the ProgressPanel, but if you have to (like in my case) you can use the FindControl(...) to get an instance of your webcontrol at runtime:
protected void Page_Load(object sender, EventArgs e)
{
Label progressMessageLabel = updateProgressMain.FindControl("lblProgressMessage") as Label;
if (progressMessageLabel != null)
{
progressMessageLabel.Text = Properties.Resources.ProgressMessage;
}
}
If anyone has an explanation why there is this behavior...your comment is very welcome :)
Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus