2

Based on my current understandings, when you have an UpdatePanel control there is no full postback. Therefore if I dynamically add custom user controls and they have UpdatePanels that are updated in their code they shouldnt disappear from the page they are loaded into, right? Apparently not. I made a simple project to test and still my dynamic controls disappear when clicked even though they should not be triggering a full postback. I have an aspx page that loads the controls:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TESTmultipleScriptManagerControls.aspx.cs" Inherits="myPlayground.TESTmultipleScriptManagerControls" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </ContentTemplate>
    </asp:UpdatePanel>

</div>
</form>
</body>
</html>

With the following code behind:

protected void Button1_Click(object sender, EventArgs e)
    {

        TESTcontrol1 temp = LoadControl("TESTcontrol1.ascx") as TESTcontrol1;
        PlaceHolder1.Controls.Add(temp);
        TESTcontrol1 temp2 = LoadControl("TESTcontrol1.ascx") as TESTcontrol1;
        PlaceHolder1.Controls.Add(temp2);

    }

And a simple user control:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TESTcontrol1.ascx.cs" Inherits="myPlayground.TESTcontrol1" %>
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">

<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>

With the following code behind:

protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = System.DateTime.Now.ToString();
    }

Any ideas on why the controls are disappearing even though there shouldnt be a postback triggering?

2 Answers 2

3

OK based on my current understandings, when you have an UpdatePanel control there is no full postback.

Postbacks triggered from UpdatePanels always execute the full page life-cycle. All the events are triggered normally. It makes no difference whether you use an UpdatePanel or not. Every time you add a control programmatically you need to re-add it on every postback.

Read this post, it may help you understand a bit better what's going on here.

3

By End of the Page Life Cycle all the controls generated at Runtime/Compile Time will be Disposed.

Below are the Page Events. Please set the BreakPoint on each Event and you can figure out that on each Asynchronous/Synchronous Request, all these Page Events are being executed.

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
}

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
}

protected override void OnPreLoad(EventArgs e)
{
    base.OnPreLoad(e);
}
protected void Page_Load(object sender, EventArgs e)
{

}
protected override void OnLoadComplete(EventArgs e)
{
    base.OnLoadComplete(e);
}

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
}

protected override void Render(HtmlTextWriter writer)
{
    base.Render(writer);
}

protected override void OnUnload(EventArgs e)
{
    base.OnUnload(e);
}

You need to populate them on each Page Init event in order to persist Viewstate, also events for controls added inside an update panel during button clicks do not seems to get registered until the next Postback. My suggestion is to keep a list of what you have added dynamically, and store it in a session variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.