Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > adding user controls to web page at runtime

Thread Tools

adding user controls to web page at runtime

Sam Kuehn
Posts: n/a
There has been a lot of articles on how to load user controls at runtime in the Init() method.
UserControl myControl = (UserControl)LoadControl(stringControl);
I add the control in the Init() method so that the viewstate to works properly. What if I want to let the user select what control to display? For instance say I have a drop down list of user controls and want to load the control the user has selected? When the user selects a control the form will auto postback. But at the point Init() method is fired the users selection in the on change event handler for the drop down list has not been set.
So here is what is happening (I think):
//this method adds control to form
//Add to session so that is available tot he Init() method
SessionManager.Instance.SomeValue = int.Parse(e.Node.DataKey.ToString());
So when a user changes a drop down list Init() is called and THEN (the problem) the session variable that tells the AddUserControl() method what control to load is set. So if I were to reload the page without changing the drop down the second time the page would display correctly. Basically the control loaded is always one postback "behind" what the user has selected. Is there any solutions to this problem? I have a lot of instances where it would be nice to load a user control on the fly and I might not know at design time what control I want to load. It my be dictated by user input. I am sure there is a best practices way to accomplish this. It seems like a problem that many people would have run into.
Many thanks in advance!
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
error in adding user controls at runtime ASP .Net Building Controls 4 03-30-2006 02:26 PM
error in adding user controls at runtime ASP .Net 4 03-30-2006 02:26 PM
Adding user controls at runtime Mantorok ASP .Net 0 05-23-2005 08:37 AM
adding user controls at runtime Sam Kuehn ASP .Net 2 11-19-2004 10:37 PM
Adding User Controls at Runtime =?Utf-8?B?TGluZGEgV2llbmhvbHQ=?= ASP .Net 1 02-09-2004 11:31 AM