Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

loading usercontrol from usercontrol

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
acadam - 27 Jun 2007 15:44 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.