LA.NET [EN]

Nov 12

Changing the contents of the UpdateProgress control

Posted in MS AJAX      Comments Off on Changing the contents of the UpdateProgress control

Today I”ve answered a question on the ASP.NET AJAX forums that  asked something like this: how do I get a reference to the controls defined inside an UpdatePanel or UpdateProgress controls? The first part is easy: just use the ID of the control. The second is not as easy as it might seem at first. The problem is associated with the way the control isntantiates the template on the PreRender event.

What this means is that you might have to write code similar to this if you want to get a reference to a control placed inside the UpdateProgress in order to change its properties:

<%@ Page Language=”C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<script runat=”server”>
    void E(Object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(2000);
    }

    private Boolean update = false;
    void J(Object sender, EventArgs e)
    {
        update = true;
    }

    protected override void  Render(HtmlTextWriter writer)
    {
        if (update)
        {
            Label lbl = (Label) progress.FindControl(“lbl”);
            lbl.Text = DateTime.Now.ToString();
        }
        base.Render(writer);
    }

</script>

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
    <title>Untitled Page</title>
</head>
<body>
    <form id=”form1″ runat=”server”>
         <asp:ScriptManager ID=”ScriptManager1″ runat=”server” />
         <asp:UpdatePanel runat=”server” ID=”panel”>
            <ContentTemplate>
                <asp:Button runat=”server” id=”bt” text=”partial postback” OnClick=”E”/>
            </ContentTemplate>
         </asp:UpdatePanel>
         <asp:Button runat=”server” text=”full postback” OnClick=”J” />
         <asp:UpdateProgress runat=”server” ID=”progress” AssociatedUpdatePanelID=”panel”>
            <ProgressTemplate>
                <asp:Label runat=”server” id=”lbl” />
            </ProgressTemplate>
         </asp:UpdateProgress>
    </form>
</body>
</html>