Accessing webcontrols inside the ProgressTemplate of an UpdateProgress
2 min read
2 min read
<asp:UpdateProgress ID="updateProgressMain" runat="server" DisplayAfter="100">then accessing the Label control "lblProgressMessage" like
<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>
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>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:
<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>
protected void Page_Load(object sender, EventArgs e)If anyone has an explanation why there is this behavior...your comment is very welcome :)
{
Label progressMessageLabel = updateProgressMain.FindControl("lblProgressMessage") as Label;
if (progressMessageLabel != null)
{
progressMessageLabel.Text = Properties.Resources.ProgressMessage;
}
}