views:

47

answers:

2

I'm developing an ASP.NET Web application with WebForms and C#.

I have this aspx page:

<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    <div id="divBody">
        <div id="divHeader">
            <asp:Button ID="Edit" runat="server" Text="Editar" onclick="Edit_Click" />
        </div>
        <div id="leftColumn">
            <asp:UpdatePanel ID="UpdatePanelLeft" runat="server">
                <ContentTemplate>
                    <asp:Label ID="TryText" runat="server"></asp:Label>
                    <asp:TreeView ID="DestinationTree" runat="server" ImageSet="Simple">
                        <ParentNodeStyle Font-Bold="False" />
                        <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
                        <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" 
                            HorizontalPadding="0px" VerticalPadding="0px" />
                        <Nodes>
                            <asp:TreeNode Text="Raiz" Value="Raiz">
                                <asp:TreeNode Text="Destino_1" Value="Destino_1">
                                    <asp:TreeNode Text="Punto_1_1" Value="Punto_1_1">
                                        <asp:TreeNode Text="Contenido_1_1_1" Value="Contenido_1_1_1"></asp:TreeNode>
                                        <asp:TreeNode Text="Contenido_1_1_2" Value="Contenido_1_1_2"></asp:TreeNode>
                                    </asp:TreeNode>
                                    <asp:TreeNode Text="Punto_1_2" Value="Punto_1_2">
                                        <asp:TreeNode Text="Contenido_1_2_1" Value="Contenido_1_2_1"></asp:TreeNode>
                                    </asp:TreeNode>
                                </asp:TreeNode>
                                <asp:TreeNode Text="Destino_2" Value="Destino_2">
                                    <asp:TreeNode Text="Punto_2_1" Value="Punto_2_1"></asp:TreeNode>
                                    <asp:TreeNode Text="Punto_2_2" Value="Punto_2_2">
                                        <asp:TreeNode Text="Contenido_2_2_1" Value="Contenido_2_2_1"></asp:TreeNode>
                                    </asp:TreeNode>
                                </asp:TreeNode>
                            </asp:TreeNode>
                        </Nodes>
                        <NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" 
                            HorizontalPadding="0px" NodeSpacing="0px" VerticalPadding="0px" />
                    </asp:TreeView>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        <div id="rightColumn">
            <asp:UpdatePanel ID="UpdatePanelRight" runat="server">
                <ContentTemplate>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Edit" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>
        </div>
    </div>
    </form>
</body>

It is a header with two columns: On left column there is a TreeView, and on the left there is nothing (now there is a label to try the thing that I want to do).

When user select a node on treeview a click on Edit button. Code for click event is:

protected void Edit_Click(object sender, EventArgs e)
{
    if ((DestinationTree.SelectedNode != null) &&
        (DestinationTree.SelectedNode.Depth > 0))
    {
        BaseControl baseControl = new BaseControl();
        UpdatePanelRight.ContentTemplateContainer.Controls.Clear();

        switch (DestinationTree.SelectedNode.Depth)
        {
            case 1:
                baseControl = (BaseControl)LoadControl("~/DynamicControls/Control1.ascx");
                break;
            case 2:
                baseControl = (BaseControl)LoadControl("~/DynamicControls/Control2.ascx");
                break;
            case 3:
                baseControl = (BaseControl)LoadControl("~/DynamicControls/Control3.ascx");
                break;
        }

        UpdatePanelRight.ContentTemplateContainer.Controls.Add(baseControl);
    }
}

Depending on node selected depth is going to load a custom control dynamically. It works, but when the user select another node on treeview, the original label is reloaded on right column.

How can I prevent this?

A: 

Dynamic controls must be re-created on every postback, this Article Here is a good link about how to persist dynamic controls and their state.

Richard Friend
Hey, you forgot the link to the article.
VansFannel
A: 

I've found this article and it works for me:

http://msdn.microsoft.com/en-us/magazine/cc748662.aspx#id0070065

VansFannel