470,594 Members | 1,101 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

loading usercontrol from usercontrol

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

Jun 27 '07 #1
2 1456
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/...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.
>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

Jun 27 '07 #2
On 27 Giu, 17:48, Yuriy Solodkyy <y.dot.solod...@gmail.comwrote:
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).

Seehttp://www.singingeels.com/Articles/Dynamically_Created_Controls_in_A...
for more details

-yuriy

Wonderful!!! This problem makes me crazy for days!!!

Thank you very much.

davides

Jun 28 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by John | last post: by
1 post views Thread by Sundaresan | last post: by
2 posts views Thread by Eric | last post: by
reply views Thread by Phl | last post: by
4 posts views Thread by Harry | last post: by
12 posts views Thread by Joe | last post: by
1 post views Thread by Nathan Sokalski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.