S
Sam Kuehn
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):
Init()
{
//this method adds control to form
AddUserControl(SessionManager.Instance.SomeValue);
}
ddl_IndexChanged()
{
//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!
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):
Init()
{
//this method adds control to form
AddUserControl(SessionManager.Instance.SomeValue);
}
ddl_IndexChanged()
{
//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!