I want to change parent...

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>
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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.
 
S

Softimax

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
 
S

Softimax

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top