LoadControl Problem

bri189a

Senior Contributor
Joined
Sep 11, 2003
Messages
1,004
Location
VA
I'm dynamically loading a control at run-time; that works great execpt for one thing. The control that is loaded is based upon the output of link button on that page, if I don't load the control until Pre-Render, the events of that control never fire. But if I load the control at OnLoad then the event that triggers which control to load never fires - don't know why. See what I'm saying? Currently I'm redirecting to the same page when the event fires so the right control gets loaded; its a terrible solution and I would like to find a more elegant, and the correct one. Any help would be appreciated.

Thanks!
 
Are you saying that when you create the controls in PreRender on OnLoad the events never fire? Have you tried using the Page Init event
 
I'll try to be more specific:

Main Page Loads (index.aspx)

This pages contains a control (indexLeftMenu.ascx) that is loaded with the page. The page has a second control in a place holder. I check a Session variable (rather use ViewState, but you'll see why that don't work in a minute). If the session is null a default control gets loaded into the place holder. Later if the user clicks one of the items on indexLeftMenu.ascx it will load a control based upon that item.

If I use the Page_Load to load the dynamically loaded control will be incorrect because the the indexLeftMenu_MenuSelected event hasn't fired yet to set the Session variable to the correct value. So when it does fire I just redirect back to the page so the correct control is loaded.

If I use the Page_PreRender event to load the dynamically loaded control the correct control is loaded, because this happens after the indexLeftMenu_MenuSelected event (which sets the session variable), but because the control is loaded at PreRender (past the stage from where ASP processes events) any events that should be processed for that dynamically loaded control won't be processed.

You can see what I'm talking about easily by just have a page and loading the control dynamically at PreRender instead of PageLoad and see how the dynmaically generated control only fires events if it was loaded at PageLoad rather than PreRender.

The cunuldrum is that to code 'correctly' I should load the control after the event fires, not before and just reloading the page like I'm currently doing.

Now I've been thinking about somehow getting the control to be loaded when the event is fired, but I'm not sure if I wanted to go that route because I don't know if the results will be differant...plus the syntax of get a controls name container to load a control from the child container might be tricky.
 
Back
Top