Oct 12, 2009, 7:14 PM
[CLOSED] Dynamically changing control hierarchy
Below is a code snippit that demonstrates a problem I'm seeing.
What I'm trying to do is dynamically change the control heirarchy after it is already built. In this case, I have a ColumnLayout with two Columns, each with a Panel. Then I want to replace one of the Panels with a FieldSet.
Here is what I'm seeing:
1) Test = 1. This works, if I make the change before the Column is added to the Layout. Either ID works.
2) Test = 2. If I use a new ID, the Panel is still rendered and not FieldSet even though I cleared that Column. It doesn't seem to know about the FieldSet. If I try to add the fieldset with the same ID as the Panel, I get an 'ID Already Initialized' exception in ScriptManger.RegiserInitID.
Obviously, when the Column is added to the Layout, the ScriptManger is doing some kind of initializaion of each ID, and keeping a list of these IDs.
I see two issues here:
1) When a control is removed, it is not de-initialized and removed from the list. Shouldn't it be?
2) Shouldn't the 'Init' of all children controls happen when the controls are added to the page's control heirarchy, rather when its just added to a parent. In this case 'layout' is not yet part of a page's control heirarchy. I thought this delayed event-catch-up was standard asp.net control lifecycle behavior? No?
In any case, I need a solution of some sort.
Thanks
What I'm trying to do is dynamically change the control heirarchy after it is already built. In this case, I have a ColumnLayout with two Columns, each with a Panel. Then I want to replace one of the Panels with a FieldSet.
Here is what I'm seeing:
1) Test = 1. This works, if I make the change before the Column is added to the Layout. Either ID works.
2) Test = 2. If I use a new ID, the Panel is still rendered and not FieldSet even though I cleared that Column. It doesn't seem to know about the FieldSet. If I try to add the fieldset with the same ID as the Panel, I get an 'ID Already Initialized' exception in ScriptManger.RegiserInitID.
Obviously, when the Column is added to the Layout, the ScriptManger is doing some kind of initializaion of each ID, and keeping a list of these IDs.
I see two issues here:
1) When a control is removed, it is not de-initialized and removed from the list. Shouldn't it be?
2) Shouldn't the 'Init' of all children controls happen when the controls are added to the page's control heirarchy, rather when its just added to a parent. In this case 'layout' is not yet part of a page's control heirarchy. I thought this delayed event-catch-up was standard asp.net control lifecycle behavior? No?
In any case, I need a solution of some sort.
Thanks
private Cool.Layout BuildLayout()
{
Cool.ColumnLayout layout = new Cool.ColumnLayout();
Cool.LayoutColumn col1 = new Cool.LayoutColumn{ColumnWidth=(decimal)0.5};
Cool.LayoutColumn col2 = new Cool.LayoutColumn{ColumnWidth=(decimal)0.5};
Cool.Panel panel1 = new Cool.Panel{BodyStyle="padding:5px;", ID="left", Title="Left Panel"};
Cool.Panel panel2 = new Cool.Panel{BodyStyle="padding:5px;", ID="right", Title="Right Panel"};
col1.Items.Add(panel1);
col2.Items.Add(panel2);
int test = 2;
bool useNewId = false;
string newId = useNewId ? "aNewId" : "right" ;
if(1 == test)
{
col2.Items.Clear();
col2.Items.Add(new FieldSet{ID=newId, Title="Right FieldSet"});
}
layout.Columns.Add(col1);
layout.Columns.Add(col2);
if(2 == test)
{
col2.Items.Clear();
col2.Items.Add(new FieldSet{ID=newId, Title="Right FieldSet"});
}
return layout;
}