In my application I have a usercontrol called "base" with a panel
called "pnl1"; in "pnl1" I put my usercontrol.
>From "base" I load another usercontrol called "ctl1".
Now I would like to have 2 possibilities: from "base" to load another
control called "ctl2" (and there's no problem) but I would like also
to load "ctl2" from "ctl1"!
The name of the control to load is "stored" into Session("activeCtl")
base.ascx Page_Load code:
Dim ctl As UserControl = New UserControl
ctl = LoadControl(Session("activeCtl") & ".ascx")
pnl1.Controls.Add(ctl)
The first time my Default.aspx set the value of Session("activeCtl").
ctl1.ascx: code put into a routine that accept a parameter called
"name" with the ascx name to load
Dim pnl1 As Panel
pnl1 = Me.Page.Parent.FindControl("pnl1")
Session("activeCtl") = name
pnl1.Controls.Clear()
Dim ctlChild As UserControl
ctlChild = Me.Page.LoadControl(name & ".ascx")
pnl1.Controls.Add(ctlChild )
also ctl2.ascx has an identical routine.
Then, coming back to explanation.
Application start: "base" is loaded
"base" loads "ctl1"
in "ctl1" there is a button that loads "ctl2"
in "ctl2" there is a button that loads "ctl1"
All seems that works fine... but if I try to click another button
(that execute normal code into control), the event doesn't fire.
Usercontrol is re-loaded another time. If I click another time the
button, the code is executed.
Why????
Sorry for my bad english... is a bit complicated to explain...
davides
Yuriy Solodkyy - 27 Jun 2007 16:48 GMT
Hi,
This happens because you do:
1. Controls.Clear
2. Controls.Add
3. You do not assign IDs to your controls
If it happens that on a single postback you clears collection of controls
and then add new controls to this collection, they get auto-IDs different
than they get on the next postback.
Try assigning ID to your dynamically created/loaded controls. Be sure that
you assign the sameID for the same control (as it is for the user).
See http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx
for more details
-yuriy
> In my application I have a usercontrol called "base" with a panel
> called "pnl1"; in "pnl1" I put my usercontrol.
[quoted text clipped - 43 lines]
>
> davides
acadam - 28 Jun 2007 08:38 GMT
> Hi,
>
[quoted text clipped - 13 lines]
>
> -yuriy
Wonderful!!! This problem makes me crazy for days!!!
Thank you very much.
davides