I want to change parent...

  • Thread starter Thread starter Softimax
  • Start date Start date
S

Softimax

Hi!
I want to change the position of a generic control. I want to append it
to a different parent.
I try some methods but I have some problem with complex controls (like
dropdown, checkboxlist)

I wrote a simple code for testing.
If you try to check something and write in textbox, after two click you
lose only checkboxes!

WHY?

Please, help me. Thanks a lot

Roberto

---------------------------------------------------------------------------

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

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CheckBoxList1.Items.Add("One");
CheckBoxList1.Items.Add("Two");
CheckBoxList1.Items.Add("Three");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Panel2.Controls.Add(Panel1);
}
protected void Button3_Click(object sender, EventArgs e)
{
Panel3.Controls.Add(Panel1);
}
</script>

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

<asp:Panel ID="Panel1" runat="server"
Height="50px" Width="125px"
BorderColor="#C0C000" BorderStyle="Solid">
<asp:CheckBoxList ID="CheckBoxList1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server" />
</asp:Panel>

<br />
<asp:Button ID="Button2" runat="server"
OnClick="Button2_Click" Text="Button" />
<br />
<asp:Panel ID="Panel2" runat="server"
BorderColor="Red" BorderStyle="Solid"
Height="50px" Width="125px">
</asp:Panel>

<br />
<asp:Button ID="Button3" runat="server"
OnClick="Button3_Click" Text="Button" />
<br />
<asp:Panel ID="Panel3" runat="server"
BorderStyle="Solid"
Height="50px" Width="125px">
</asp:Panel>

</form>
</body>
</html>
 
You can't take a panel that is part of the page and just add it
somewhere else in the page. You will end up with having two controls
that think that the panel is their child, but the panel will only think
that one of them is the parent.

If you want to move a control in the page, you have to first remove it
from the original parent's Controls collection.
 
Your solution doesn't work
I update the original code with:

protected void Button2_Click(object sender, EventArgs e)
{
Panel1.Parent.Controls.Remove(Panel1);
Panel2.Controls.Add(Panel1);
}
protected void Button3_Click(object sender, EventArgs e)
{
Panel1.Parent.Controls.Remove(Panel1);
Panel3.Controls.Add(Panel1);
}

but the result is the same.

I also try "deep clone" of the panel, but I have only tons of errors

Thanks
 
I try also this:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CheckBoxList1.Items.Add("One");
CheckBoxList1.Items.Add("Two");
CheckBoxList1.Items.Add("Three");
}

if (ViewState["panel"] != null)
{
Panel1.Parent.Controls.Remove(Panel1);
if ( ((int)ViewState["panel"])==2)
Panel2.Controls.Add(Panel1);
else if ( ((int)ViewState["panel"])==3)
Panel3.Controls.Add(Panel1);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Panel1.Parent.Controls.Remove(Panel1);
Panel2.Controls.Add(Panel1);
ViewState["panel"] = 2;
}
protected void Button3_Click(object sender, EventArgs e)
{
Panel1.Parent.Controls.Remove(Panel1);
Panel3.Controls.Add(Panel1);
ViewState["panel"] = 3;
}

but without results.

Help me!
 
Back
Top